2caff8e980f8d3400167228c5b28c99abe105898
[reactos.git] / rosapps / applications / winfile / winefile.c
1 /*
2 * Winefile
3 *
4 * Copyright 2000, 2003, 2004, 2005 Martin Fuchs
5 * Copyright 2006 Jason Green
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21
22 #ifdef __WINE__
23 #include "config.h"
24 #include "wine/port.h"
25
26 /* for unix filesystem function calls */
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <dirent.h>
30 #endif
31
32 #define COBJMACROS
33
34 #include "winefile.h"
35 #include "resource.h"
36 #include "wine/unicode.h"
37
38 #ifdef _NO_EXTENSIONS
39 #undef _LEFT_FILES
40 #endif
41
42 #ifndef _MAX_PATH
43 #define _MAX_DRIVE 3
44 #define _MAX_FNAME 256
45 #define _MAX_DIR _MAX_FNAME
46 #define _MAX_EXT _MAX_FNAME
47 #define _MAX_PATH 260
48 #endif
49
50 #ifdef NONAMELESSUNION
51 #define UNION_MEMBER(x) DUMMYUNIONNAME.x
52 #else
53 #define UNION_MEMBER(x) x
54 #endif
55
56
57 #ifdef _SHELL_FOLDERS
58 #define DEFAULT_SPLIT_POS 300
59 #else
60 #define DEFAULT_SPLIT_POS 200
61 #endif
62
63 static const WCHAR registry_key[] = { 'S','o','f','t','w','a','r','e','\\',
64 'W','i','n','e','\\',
65 'W','i','n','e','F','i','l','e','\0'};
66 static const WCHAR reg_start_x[] = { 's','t','a','r','t','X','\0'};
67 static const WCHAR reg_start_y[] = { 's','t','a','r','t','Y','\0'};
68 static const WCHAR reg_width[] = { 'w','i','d','t','h','\0'};
69 static const WCHAR reg_height[] = { 'h','e','i','g','h','t','\0'};
70 static const WCHAR reg_logfont[] = { 'l','o','g','f','o','n','t','\0'};
71
72 enum ENTRY_TYPE {
73 ET_WINDOWS,
74 ET_UNIX,
75 #ifdef _SHELL_FOLDERS
76 ET_SHELL
77 #endif
78 };
79
80 typedef struct _Entry {
81 struct _Entry* next;
82 struct _Entry* down;
83 struct _Entry* up;
84
85 BOOL expanded;
86 BOOL scanned;
87 int level;
88
89 WIN32_FIND_DATA data;
90
91 #ifndef _NO_EXTENSIONS
92 BY_HANDLE_FILE_INFORMATION bhfi;
93 BOOL bhfi_valid;
94 enum ENTRY_TYPE etype;
95 #endif
96 #ifdef _SHELL_FOLDERS
97 LPITEMIDLIST pidl;
98 IShellFolder* folder;
99 HICON hicon;
100 #endif
101 } Entry;
102
103 typedef struct {
104 Entry entry;
105 TCHAR path[MAX_PATH];
106 TCHAR volname[_MAX_FNAME];
107 TCHAR fs[_MAX_DIR];
108 DWORD drive_type;
109 DWORD fs_flags;
110 } Root;
111
112 enum COLUMN_FLAGS {
113 COL_SIZE = 0x01,
114 COL_DATE = 0x02,
115 COL_TIME = 0x04,
116 COL_ATTRIBUTES = 0x08,
117 COL_DOSNAMES = 0x10,
118 #ifdef _NO_EXTENSIONS
119 COL_ALL = COL_SIZE|COL_DATE|COL_TIME|COL_ATTRIBUTES|COL_DOSNAMES
120 #else
121 COL_INDEX = 0x20,
122 COL_LINKS = 0x40,
123 COL_ALL = COL_SIZE|COL_DATE|COL_TIME|COL_ATTRIBUTES|COL_DOSNAMES|COL_INDEX|COL_LINKS
124 #endif
125 };
126
127 typedef enum {
128 SORT_NAME,
129 SORT_EXT,
130 SORT_SIZE,
131 SORT_DATE
132 } SORT_ORDER;
133
134 typedef struct {
135 HWND hwnd;
136 #ifndef _NO_EXTENSIONS
137 HWND hwndHeader;
138 #endif
139
140 #ifndef _NO_EXTENSIONS
141 #define COLUMNS 10
142 #else
143 #define COLUMNS 5
144 #endif
145 int widths[COLUMNS];
146 int positions[COLUMNS+1];
147
148 BOOL treePane;
149 int visible_cols;
150 Entry* root;
151 Entry* cur;
152 } Pane;
153
154 typedef struct {
155 HWND hwnd;
156 Pane left;
157 Pane right;
158 int focus_pane; /* 0: left 1: right */
159 WINDOWPLACEMENT pos;
160 int split_pos;
161 BOOL header_wdths_ok;
162
163 TCHAR path[MAX_PATH];
164 TCHAR filter_pattern[MAX_PATH];
165 int filter_flags;
166 Root root;
167
168 SORT_ORDER sortOrder;
169 } ChildWnd;
170
171
172
173 static void read_directory(Entry* dir, LPCTSTR path, SORT_ORDER sortOrder, HWND hwnd);
174 static void set_curdir(ChildWnd* child, Entry* entry, int idx, HWND hwnd);
175 static void refresh_child(ChildWnd* child);
176 static void refresh_drives(void);
177 static void get_path(Entry* dir, PTSTR path);
178 static void format_date(const FILETIME* ft, TCHAR* buffer, int visible_cols);
179
180 static LRESULT CALLBACK FrameWndProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam);
181 static LRESULT CALLBACK ChildWndProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam);
182 static LRESULT CALLBACK TreeWndProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam);
183
184
185 /* globals */
186 WINEFILE_GLOBALS Globals;
187
188 static int last_split;
189
190 /* some common string constants */
191 static const TCHAR sEmpty[] = {'\0'};
192 static const WCHAR sSpace[] = {' ', '\0'};
193 static const TCHAR sNumFmt[] = {'%','d','\0'};
194 static const TCHAR sQMarks[] = {'?','?','?','\0'};
195
196 /* window class names */
197 static const TCHAR sWINEFILEFRAME[] = {'W','F','S','_','F','r','a','m','e','\0'};
198 static const TCHAR sWINEFILETREE[] = {'W','F','S','_','T','r','e','e','\0'};
199
200 static void format_longlong(LPWSTR ret, ULONGLONG val)
201 {
202 WCHAR buffer[65], *p = &buffer[64];
203
204 *p = 0;
205 do {
206 *(--p) = '0' + val % 10;
207 val /= 10;
208 } while (val);
209 lstrcpyW( ret, p );
210 }
211
212
213 /* load resource string */
214 static LPTSTR load_string(LPTSTR buffer, DWORD size, UINT id)
215 {
216 LoadString(Globals.hInstance, id, buffer, size);
217 return buffer;
218 }
219
220 #define RS(b, i) load_string(b, sizeof(b)/sizeof(b[0]), i)
221
222
223 /* display error message for the specified WIN32 error code */
224 static void display_error(HWND hwnd, DWORD error)
225 {
226 TCHAR b1[BUFFER_LEN], b2[BUFFER_LEN];
227 PTSTR msg;
228
229 if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
230 0, error, MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), (PTSTR)&msg, 0, NULL))
231 MessageBox(hwnd, msg, RS(b2,IDS_WINEFILE), MB_OK);
232 else
233 MessageBox(hwnd, RS(b1,IDS_ERROR), RS(b2,IDS_WINEFILE), MB_OK);
234
235 LocalFree(msg);
236 }
237
238
239 /* display network error message using WNetGetLastError() */
240 static void display_network_error(HWND hwnd)
241 {
242 TCHAR msg[BUFFER_LEN], provider[BUFFER_LEN], b2[BUFFER_LEN];
243 DWORD error;
244
245 if (WNetGetLastError(&error, msg, BUFFER_LEN, provider, BUFFER_LEN) == NO_ERROR)
246 MessageBox(hwnd, msg, RS(b2,IDS_WINEFILE), MB_OK);
247 }
248
249 static inline BOOL get_check(HWND hwnd, INT id)
250 {
251 return BST_CHECKED&SendMessageW(GetDlgItem(hwnd, id), BM_GETSTATE, 0, 0);
252 }
253
254 static inline INT set_check(HWND hwnd, INT id, BOOL on)
255 {
256 return SendMessageW(GetDlgItem(hwnd, id), BM_SETCHECK, on?BST_CHECKED:BST_UNCHECKED, 0);
257 }
258
259 static inline void choose_font(HWND hwnd)
260 {
261 WCHAR dlg_name[BUFFER_LEN], dlg_info[BUFFER_LEN];
262 CHOOSEFONTW chFont;
263 LOGFONTW lFont;
264
265 HDC hdc = GetDC(hwnd);
266 chFont.lStructSize = sizeof(CHOOSEFONT);
267 chFont.hwndOwner = hwnd;
268 chFont.hDC = NULL;
269 chFont.lpLogFont = &lFont;
270 chFont.Flags = CF_SCREENFONTS | CF_FORCEFONTEXIST | CF_LIMITSIZE | CF_NOSCRIPTSEL;
271 chFont.rgbColors = RGB(0,0,0);
272 chFont.lCustData = 0;
273 chFont.lpfnHook = NULL;
274 chFont.lpTemplateName = NULL;
275 chFont.hInstance = Globals.hInstance;
276 chFont.lpszStyle = NULL;
277 chFont.nFontType = SIMULATED_FONTTYPE;
278 chFont.nSizeMin = 0;
279 chFont.nSizeMax = 24;
280
281 if (ChooseFontW(&chFont)) {
282 HWND childWnd;
283 HFONT hFontOld;
284
285 DeleteObject(Globals.hfont);
286 Globals.hfont = CreateFontIndirectW(&lFont);
287 hFontOld = SelectObject(hdc, Globals.hfont);
288 GetTextExtentPoint32W(hdc, sSpace, 1, &Globals.spaceSize);
289
290 /* change font in all open child windows */
291 for(childWnd=GetWindow(Globals.hmdiclient,GW_CHILD); childWnd; childWnd=GetNextWindow(childWnd,GW_HWNDNEXT)) {
292 ChildWnd* child = (ChildWnd*) GetWindowLongPtrW(childWnd, GWLP_USERDATA);
293 SendMessageW(child->left.hwnd, WM_SETFONT, (WPARAM)Globals.hfont, TRUE);
294 SendMessageW(child->right.hwnd, WM_SETFONT, (WPARAM)Globals.hfont, TRUE);
295 SendMessageW(child->left.hwnd, LB_SETITEMHEIGHT, 1, max(Globals.spaceSize.cy,IMAGE_HEIGHT+3));
296 SendMessageW(child->right.hwnd, LB_SETITEMHEIGHT, 1, max(Globals.spaceSize.cy,IMAGE_HEIGHT+3));
297 InvalidateRect(child->left.hwnd, NULL, TRUE);
298 InvalidateRect(child->right.hwnd, NULL, TRUE);
299 }
300
301 SelectObject(hdc, hFontOld);
302 }
303 else if (CommDlgExtendedError()) {
304 LoadStringW(Globals.hInstance, IDS_FONT_SEL_DLG_NAME, dlg_name, BUFFER_LEN);
305 LoadStringW(Globals.hInstance, IDS_FONT_SEL_ERROR, dlg_info, BUFFER_LEN);
306 MessageBoxW(hwnd, dlg_info, dlg_name, MB_OK);
307 }
308
309 ReleaseDC(hwnd, hdc);
310 }
311
312
313 /* allocate and initialise a directory entry */
314 static Entry* alloc_entry(void)
315 {
316 Entry* entry = HeapAlloc(GetProcessHeap(), 0, sizeof(Entry));
317
318 #ifdef _SHELL_FOLDERS
319 entry->pidl = NULL;
320 entry->folder = NULL;
321 entry->hicon = 0;
322 #endif
323
324 return entry;
325 }
326
327 /* free a directory entry */
328 static void free_entry(Entry* entry)
329 {
330 #ifdef _SHELL_FOLDERS
331 if (entry->hicon && entry->hicon!=(HICON)-1)
332 DestroyIcon(entry->hicon);
333
334 if (entry->folder && entry->folder!=Globals.iDesktop)
335 IShellFolder_Release(entry->folder);
336
337 if (entry->pidl)
338 IMalloc_Free(Globals.iMalloc, entry->pidl);
339 #endif
340
341 HeapFree(GetProcessHeap(), 0, entry);
342 }
343
344 /* recursively free all child entries */
345 static void free_entries(Entry* dir)
346 {
347 Entry *entry, *next=dir->down;
348
349 if (next) {
350 dir->down = 0;
351
352 do {
353 entry = next;
354 next = entry->next;
355
356 free_entries(entry);
357 free_entry(entry);
358 } while(next);
359 }
360 }
361
362
363 static void read_directory_win(Entry* dir, LPCTSTR path)
364 {
365 Entry* first_entry = NULL;
366 Entry* last = NULL;
367 Entry* entry;
368
369 int level = dir->level + 1;
370 WIN32_FIND_DATA w32fd;
371 HANDLE hFind;
372 #ifndef _NO_EXTENSIONS
373 HANDLE hFile;
374 #endif
375
376 TCHAR buffer[MAX_PATH], *p;
377 for(p=buffer; *path; )
378 *p++ = *path++;
379
380 *p++ = '\\';
381 p[0] = '*';
382 p[1] = '\0';
383
384 hFind = FindFirstFile(buffer, &w32fd);
385
386 if (hFind != INVALID_HANDLE_VALUE) {
387 do {
388 #ifdef _NO_EXTENSIONS
389 /* hide directory entry "." */
390 if (w32fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
391 LPCTSTR name = w32fd.cFileName;
392
393 if (name[0]=='.' && name[1]=='\0')
394 continue;
395 }
396 #endif
397 entry = alloc_entry();
398
399 if (!first_entry)
400 first_entry = entry;
401
402 if (last)
403 last->next = entry;
404
405 memcpy(&entry->data, &w32fd, sizeof(WIN32_FIND_DATA));
406 entry->down = NULL;
407 entry->up = dir;
408 entry->expanded = FALSE;
409 entry->scanned = FALSE;
410 entry->level = level;
411
412 #ifndef _NO_EXTENSIONS
413 entry->etype = ET_WINDOWS;
414 entry->bhfi_valid = FALSE;
415
416 lstrcpy(p, entry->data.cFileName);
417
418 hFile = CreateFile(buffer, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
419 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0);
420
421 if (hFile != INVALID_HANDLE_VALUE) {
422 if (GetFileInformationByHandle(hFile, &entry->bhfi))
423 entry->bhfi_valid = TRUE;
424
425 CloseHandle(hFile);
426 }
427 #endif
428
429 last = entry;
430 } while(FindNextFile(hFind, &w32fd));
431
432 if (last)
433 last->next = NULL;
434
435 FindClose(hFind);
436 }
437
438 dir->down = first_entry;
439 dir->scanned = TRUE;
440 }
441
442
443 static Entry* find_entry_win(Entry* dir, LPCTSTR name)
444 {
445 Entry* entry;
446
447 for(entry=dir->down; entry; entry=entry->next) {
448 LPCTSTR p = name;
449 LPCTSTR q = entry->data.cFileName;
450
451 do {
452 if (!*p || *p == '\\' || *p == '/')
453 return entry;
454 } while(tolower(*p++) == tolower(*q++));
455
456 p = name;
457 q = entry->data.cAlternateFileName;
458
459 do {
460 if (!*p || *p == '\\' || *p == '/')
461 return entry;
462 } while(tolower(*p++) == tolower(*q++));
463 }
464
465 return 0;
466 }
467
468
469 static Entry* read_tree_win(Root* root, LPCTSTR path, SORT_ORDER sortOrder, HWND hwnd)
470 {
471 TCHAR buffer[MAX_PATH];
472 Entry* entry = &root->entry;
473 LPCTSTR s = path;
474 PTSTR d = buffer;
475
476 HCURSOR old_cursor = SetCursor(LoadCursor(0, IDC_WAIT));
477
478 #ifndef _NO_EXTENSIONS
479 entry->etype = ET_WINDOWS;
480 #endif
481
482 while(entry) {
483 while(*s && *s != '\\' && *s != '/')
484 *d++ = *s++;
485
486 while(*s == '\\' || *s == '/')
487 s++;
488
489 *d++ = '\\';
490 *d = '\0';
491
492 read_directory(entry, buffer, sortOrder, hwnd);
493
494 if (entry->down)
495 entry->expanded = TRUE;
496
497 if (!*s)
498 break;
499
500 entry = find_entry_win(entry, s);
501 }
502
503 SetCursor(old_cursor);
504
505 return entry;
506 }
507
508
509 #if !defined(_NO_EXTENSIONS) && defined(__WINE__)
510
511 static BOOL time_to_filetime(const time_t* t, FILETIME* ftime)
512 {
513 struct tm* tm = gmtime(t);
514 SYSTEMTIME stime;
515
516 if (!tm)
517 return FALSE;
518
519 stime.wYear = tm->tm_year+1900;
520 stime.wMonth = tm->tm_mon+1;
521 /* stime.wDayOfWeek */
522 stime.wDay = tm->tm_mday;
523 stime.wHour = tm->tm_hour;
524 stime.wMinute = tm->tm_min;
525 stime.wSecond = tm->tm_sec;
526
527 return SystemTimeToFileTime(&stime, ftime);
528 }
529
530 static void read_directory_unix(Entry* dir, LPCTSTR path)
531 {
532 Entry* first_entry = NULL;
533 Entry* last = NULL;
534 Entry* entry;
535 DIR* pdir;
536
537 int level = dir->level + 1;
538 #ifdef UNICODE
539 char cpath[MAX_PATH];
540
541 WideCharToMultiByte(CP_UNIXCP, 0, path, -1, cpath, MAX_PATH, NULL, NULL);
542 #else
543 const char* cpath = path;
544 #endif
545
546 pdir = opendir(cpath);
547
548 if (pdir) {
549 struct stat st;
550 struct dirent* ent;
551 char buffer[MAX_PATH], *p;
552 const char* s;
553
554 for(p=buffer,s=cpath; *s; )
555 *p++ = *s++;
556
557 if (p==buffer || p[-1]!='/')
558 *p++ = '/';
559
560 while((ent=readdir(pdir))) {
561 entry = alloc_entry();
562
563 if (!first_entry)
564 first_entry = entry;
565
566 if (last)
567 last->next = entry;
568
569 entry->etype = ET_UNIX;
570
571 strcpy(p, ent->d_name);
572 #ifdef UNICODE
573 MultiByteToWideChar(CP_UNIXCP, 0, p, -1, entry->data.cFileName, MAX_PATH);
574 #else
575 lstrcpy(entry->data.cFileName, p);
576 #endif
577
578 if (!stat(buffer, &st)) {
579 entry->data.dwFileAttributes = p[0]=='.'? FILE_ATTRIBUTE_HIDDEN: 0;
580
581 if (S_ISDIR(st.st_mode))
582 entry->data.dwFileAttributes |= FILE_ATTRIBUTE_DIRECTORY;
583
584 entry->data.nFileSizeLow = st.st_size & 0xFFFFFFFF;
585 entry->data.nFileSizeHigh = st.st_size >> 32;
586
587 memset(&entry->data.ftCreationTime, 0, sizeof(FILETIME));
588 time_to_filetime(&st.st_atime, &entry->data.ftLastAccessTime);
589 time_to_filetime(&st.st_mtime, &entry->data.ftLastWriteTime);
590
591 entry->bhfi.nFileIndexLow = ent->d_ino;
592 entry->bhfi.nFileIndexHigh = 0;
593
594 entry->bhfi.nNumberOfLinks = st.st_nlink;
595
596 entry->bhfi_valid = TRUE;
597 } else {
598 entry->data.nFileSizeLow = 0;
599 entry->data.nFileSizeHigh = 0;
600 entry->bhfi_valid = FALSE;
601 }
602
603 entry->down = NULL;
604 entry->up = dir;
605 entry->expanded = FALSE;
606 entry->scanned = FALSE;
607 entry->level = level;
608
609 last = entry;
610 }
611
612 if (last)
613 last->next = NULL;
614
615 closedir(pdir);
616 }
617
618 dir->down = first_entry;
619 dir->scanned = TRUE;
620 }
621
622 static Entry* find_entry_unix(Entry* dir, LPCTSTR name)
623 {
624 Entry* entry;
625
626 for(entry=dir->down; entry; entry=entry->next) {
627 LPCTSTR p = name;
628 LPCTSTR q = entry->data.cFileName;
629
630 do {
631 if (!*p || *p == '/')
632 return entry;
633 } while(*p++ == *q++);
634 }
635
636 return 0;
637 }
638
639 static Entry* read_tree_unix(Root* root, LPCTSTR path, SORT_ORDER sortOrder, HWND hwnd)
640 {
641 TCHAR buffer[MAX_PATH];
642 Entry* entry = &root->entry;
643 LPCTSTR s = path;
644 PTSTR d = buffer;
645
646 HCURSOR old_cursor = SetCursor(LoadCursor(0, IDC_WAIT));
647
648 entry->etype = ET_UNIX;
649
650 while(entry) {
651 while(*s && *s != '/')
652 *d++ = *s++;
653
654 while(*s == '/')
655 s++;
656
657 *d++ = '/';
658 *d = '\0';
659
660 read_directory(entry, buffer, sortOrder, hwnd);
661
662 if (entry->down)
663 entry->expanded = TRUE;
664
665 if (!*s)
666 break;
667
668 entry = find_entry_unix(entry, s);
669 }
670
671 SetCursor(old_cursor);
672
673 return entry;
674 }
675
676 #endif /* !defined(_NO_EXTENSIONS) && defined(__WINE__) */
677
678
679 #ifdef _SHELL_FOLDERS
680
681 #ifdef UNICODE
682 #define get_strret get_strretW
683 #define path_from_pidl path_from_pidlW
684 #else
685 #define get_strret get_strretA
686 #define path_from_pidl path_from_pidlA
687 #endif
688
689
690 static void free_strret(STRRET* str)
691 {
692 if (str->uType == STRRET_WSTR)
693 IMalloc_Free(Globals.iMalloc, str->UNION_MEMBER(pOleStr));
694 }
695
696
697 #ifndef UNICODE
698
699 static LPSTR strcpyn(LPSTR dest, LPCSTR source, size_t count)
700 {
701 LPCSTR s;
702 LPSTR d = dest;
703
704 for(s=source; count&&(*d++=*s++); )
705 count--;
706
707 return dest;
708 }
709
710 static void get_strretA(STRRET* str, const SHITEMID* shiid, LPSTR buffer, int len)
711 {
712 switch(str->uType) {
713 case STRRET_WSTR:
714 WideCharToMultiByte(CP_ACP, 0, str->UNION_MEMBER(pOleStr), -1, buffer, len, NULL, NULL);
715 break;
716
717 case STRRET_OFFSET:
718 strcpyn(buffer, (LPCSTR)shiid+str->UNION_MEMBER(uOffset), len);
719 break;
720
721 case STRRET_CSTR:
722 strcpyn(buffer, str->UNION_MEMBER(cStr), len);
723 }
724 }
725
726 static HRESULT path_from_pidlA(IShellFolder* folder, LPITEMIDLIST pidl, LPSTR buffer, int len)
727 {
728 STRRET str;
729
730 /* SHGDN_FORPARSING: get full path of id list */
731 HRESULT hr = IShellFolder_GetDisplayNameOf(folder, pidl, SHGDN_FORPARSING, &str);
732
733 if (SUCCEEDED(hr)) {
734 get_strretA(&str, &pidl->mkid, buffer, len);
735 free_strret(&str);
736 } else
737 buffer[0] = '\0';
738
739 return hr;
740 }
741
742 #endif
743
744 static LPWSTR wcscpyn(LPWSTR dest, LPCWSTR source, size_t count)
745 {
746 LPCWSTR s;
747 LPWSTR d = dest;
748
749 for(s=source; count&&(*d++=*s++); )
750 count--;
751
752 return dest;
753 }
754
755 static void get_strretW(STRRET* str, const SHITEMID* shiid, LPWSTR buffer, int len)
756 {
757 switch(str->uType) {
758 case STRRET_WSTR:
759 wcscpyn(buffer, str->UNION_MEMBER(pOleStr), len);
760 break;
761
762 case STRRET_OFFSET:
763 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)shiid+str->UNION_MEMBER(uOffset), -1, buffer, len);
764 break;
765
766 case STRRET_CSTR:
767 MultiByteToWideChar(CP_ACP, 0, str->UNION_MEMBER(cStr), -1, buffer, len);
768 }
769 }
770
771
772 static HRESULT name_from_pidl(IShellFolder* folder, LPITEMIDLIST pidl, LPTSTR buffer, int len, SHGDNF flags)
773 {
774 STRRET str;
775
776 HRESULT hr = IShellFolder_GetDisplayNameOf(folder, pidl, flags, &str);
777
778 if (SUCCEEDED(hr)) {
779 get_strret(&str, &pidl->mkid, buffer, len);
780 free_strret(&str);
781 } else
782 buffer[0] = '\0';
783
784 return hr;
785 }
786
787
788 static HRESULT path_from_pidlW(IShellFolder* folder, LPITEMIDLIST pidl, LPWSTR buffer, int len)
789 {
790 STRRET str;
791
792 /* SHGDN_FORPARSING: get full path of id list */
793 HRESULT hr = IShellFolder_GetDisplayNameOf(folder, pidl, SHGDN_FORPARSING, &str);
794
795 if (SUCCEEDED(hr)) {
796 get_strretW(&str, &pidl->mkid, buffer, len);
797 free_strret(&str);
798 } else
799 buffer[0] = '\0';
800
801 return hr;
802 }
803
804
805 /* create an item id list from a file system path */
806
807 static LPITEMIDLIST get_path_pidl(LPTSTR path, HWND hwnd)
808 {
809 LPITEMIDLIST pidl;
810 HRESULT hr;
811 ULONG len;
812
813 #ifdef UNICODE
814 LPWSTR buffer = path;
815 #else
816 WCHAR buffer[MAX_PATH];
817 MultiByteToWideChar(CP_ACP, 0, path, -1, buffer, MAX_PATH);
818 #endif
819
820 hr = IShellFolder_ParseDisplayName(Globals.iDesktop, hwnd, NULL, buffer, &len, &pidl, NULL);
821 if (FAILED(hr))
822 return NULL;
823
824 return pidl;
825 }
826
827
828 /* convert an item id list from relative to absolute (=relative to the desktop) format */
829
830 static LPITEMIDLIST get_to_absolute_pidl(Entry* entry, HWND hwnd)
831 {
832 if (entry->up && entry->up->etype==ET_SHELL) {
833 LPITEMIDLIST idl = NULL;
834
835 while (entry->up) {
836 idl = ILCombine(ILClone(entry->pidl), idl);
837 entry = entry->up;
838 }
839
840 return idl;
841 } else if (entry->etype == ET_WINDOWS) {
842 TCHAR path[MAX_PATH];
843
844 get_path(entry, path);
845
846 return get_path_pidl(path, hwnd);
847 } else if (entry->pidl)
848 return ILClone(entry->pidl);
849
850 return NULL;
851 }
852
853
854 static HICON extract_icon(IShellFolder* folder, LPCITEMIDLIST pidl)
855 {
856 IExtractIcon* pExtract;
857
858 if (SUCCEEDED(IShellFolder_GetUIObjectOf(folder, 0, 1, (LPCITEMIDLIST*)&pidl, &IID_IExtractIcon, 0, (LPVOID*)&pExtract))) {
859 TCHAR path[_MAX_PATH];
860 unsigned flags;
861 HICON hicon;
862 int idx;
863
864 if (SUCCEEDED(IExtractIconW_GetIconLocation(pExtract, GIL_FORSHELL, path, _MAX_PATH, &idx, &flags))) {
865 if (!(flags & GIL_NOTFILENAME)) {
866 if (idx == -1)
867 idx = 0; /* special case for some control panel applications */
868
869 if ((int)ExtractIconEx(path, idx, 0, &hicon, 1) > 0)
870 flags &= ~GIL_DONTCACHE;
871 } else {
872 HICON hIconLarge = 0;
873
874 HRESULT hr = IExtractIconW_Extract(pExtract, path, idx, &hIconLarge, &hicon, MAKELONG(0/*GetSystemMetrics(SM_CXICON)*/,GetSystemMetrics(SM_CXSMICON)));
875
876 if (SUCCEEDED(hr))
877 DestroyIcon(hIconLarge);
878 }
879
880 return hicon;
881 }
882 }
883
884 return 0;
885 }
886
887
888 static Entry* find_entry_shell(Entry* dir, LPCITEMIDLIST pidl)
889 {
890 Entry* entry;
891
892 for(entry=dir->down; entry; entry=entry->next) {
893 if (entry->pidl->mkid.cb == pidl->mkid.cb &&
894 !memcmp(entry->pidl, pidl, entry->pidl->mkid.cb))
895 return entry;
896 }
897
898 return 0;
899 }
900
901 static Entry* read_tree_shell(Root* root, LPITEMIDLIST pidl, SORT_ORDER sortOrder, HWND hwnd)
902 {
903 Entry* entry = &root->entry;
904 Entry* next;
905 LPITEMIDLIST next_pidl = pidl;
906 IShellFolder* folder;
907 IShellFolder* child = NULL;
908 HRESULT hr;
909
910 HCURSOR old_cursor = SetCursor(LoadCursor(0, IDC_WAIT));
911
912 #ifndef _NO_EXTENSIONS
913 entry->etype = ET_SHELL;
914 #endif
915
916 folder = Globals.iDesktop;
917
918 while(entry) {
919 entry->pidl = next_pidl;
920 entry->folder = folder;
921
922 if (!pidl->mkid.cb)
923 break;
924
925 /* copy first element of item idlist */
926 next_pidl = IMalloc_Alloc(Globals.iMalloc, pidl->mkid.cb+sizeof(USHORT));
927 memcpy(next_pidl, pidl, pidl->mkid.cb);
928 ((LPITEMIDLIST)((LPBYTE)next_pidl+pidl->mkid.cb))->mkid.cb = 0;
929
930 hr = IShellFolder_BindToObject(folder, next_pidl, 0, &IID_IShellFolder, (void**)&child);
931 if (FAILED(hr))
932 break;
933
934 read_directory(entry, NULL, sortOrder, hwnd);
935
936 if (entry->down)
937 entry->expanded = TRUE;
938
939 next = find_entry_shell(entry, next_pidl);
940 if (!next)
941 break;
942
943 folder = child;
944 entry = next;
945
946 /* go to next element */
947 pidl = (LPITEMIDLIST) ((LPBYTE)pidl+pidl->mkid.cb);
948 }
949
950 SetCursor(old_cursor);
951
952 return entry;
953 }
954
955
956 static void fill_w32fdata_shell(IShellFolder* folder, LPCITEMIDLIST pidl, SFGAOF attribs, WIN32_FIND_DATA* w32fdata)
957 {
958 if (!(attribs & SFGAO_FILESYSTEM) ||
959 FAILED(SHGetDataFromIDList(folder, pidl, SHGDFIL_FINDDATA, w32fdata, sizeof(WIN32_FIND_DATA)))) {
960 WIN32_FILE_ATTRIBUTE_DATA fad;
961 IDataObject* pDataObj;
962
963 STGMEDIUM medium = {0, {0}, 0};
964 FORMATETC fmt = {Globals.cfStrFName, 0, DVASPECT_CONTENT, -1, TYMED_HGLOBAL};
965
966 HRESULT hr = IShellFolder_GetUIObjectOf(folder, 0, 1, &pidl, &IID_IDataObject, 0, (LPVOID*)&pDataObj);
967
968 if (SUCCEEDED(hr)) {
969 hr = IDataObject_GetData(pDataObj, &fmt, &medium);
970
971 IDataObject_Release(pDataObj);
972
973 if (SUCCEEDED(hr)) {
974 LPCTSTR path = (LPCTSTR)GlobalLock(medium.UNION_MEMBER(hGlobal));
975 UINT sem_org = SetErrorMode(SEM_FAILCRITICALERRORS);
976
977 if (GetFileAttributesEx(path, GetFileExInfoStandard, &fad)) {
978 w32fdata->dwFileAttributes = fad.dwFileAttributes;
979 w32fdata->ftCreationTime = fad.ftCreationTime;
980 w32fdata->ftLastAccessTime = fad.ftLastAccessTime;
981 w32fdata->ftLastWriteTime = fad.ftLastWriteTime;
982
983 if (!(fad.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
984 w32fdata->nFileSizeLow = fad.nFileSizeLow;
985 w32fdata->nFileSizeHigh = fad.nFileSizeHigh;
986 }
987 }
988
989 SetErrorMode(sem_org);
990
991 GlobalUnlock(medium.UNION_MEMBER(hGlobal));
992 GlobalFree(medium.UNION_MEMBER(hGlobal));
993 }
994 }
995 }
996
997 if (attribs & (SFGAO_FOLDER|SFGAO_HASSUBFOLDER))
998 w32fdata->dwFileAttributes |= FILE_ATTRIBUTE_DIRECTORY;
999
1000 if (attribs & SFGAO_READONLY)
1001 w32fdata->dwFileAttributes |= FILE_ATTRIBUTE_READONLY;
1002
1003 if (attribs & SFGAO_COMPRESSED)
1004 w32fdata->dwFileAttributes |= FILE_ATTRIBUTE_COMPRESSED;
1005 }
1006
1007
1008 static void read_directory_shell(Entry* dir, HWND hwnd)
1009 {
1010 IShellFolder* folder = dir->folder;
1011 int level = dir->level + 1;
1012 HRESULT hr;
1013
1014 IShellFolder* child;
1015 IEnumIDList* idlist;
1016
1017 Entry* first_entry = NULL;
1018 Entry* last = NULL;
1019 Entry* entry;
1020
1021 if (!folder)
1022 return;
1023
1024 hr = IShellFolder_EnumObjects(folder, hwnd, SHCONTF_FOLDERS|SHCONTF_NONFOLDERS|SHCONTF_INCLUDEHIDDEN|SHCONTF_SHAREABLE|SHCONTF_STORAGE, &idlist);
1025
1026 if (SUCCEEDED(hr)) {
1027 for(;;) {
1028 #define FETCH_ITEM_COUNT 32
1029 LPITEMIDLIST pidls[FETCH_ITEM_COUNT];
1030 SFGAOF attribs;
1031 ULONG cnt = 0;
1032 ULONG n;
1033
1034 memset(pidls, 0, sizeof(pidls));
1035
1036 hr = IEnumIDList_Next(idlist, FETCH_ITEM_COUNT, pidls, &cnt);
1037 if (FAILED(hr))
1038 break;
1039
1040 if (hr == S_FALSE)
1041 break;
1042
1043 for(n=0; n<cnt; ++n) {
1044 entry = alloc_entry();
1045
1046 if (!first_entry)
1047 first_entry = entry;
1048
1049 if (last)
1050 last->next = entry;
1051
1052 memset(&entry->data, 0, sizeof(WIN32_FIND_DATA));
1053 entry->bhfi_valid = FALSE;
1054
1055 attribs = ~SFGAO_FILESYSTEM; /*SFGAO_HASSUBFOLDER|SFGAO_FOLDER; SFGAO_FILESYSTEM sorgt dafür, daß "My Documents" anstatt von "Martin's Documents" angezeigt wird */
1056
1057 hr = IShellFolder_GetAttributesOf(folder, 1, (LPCITEMIDLIST*)&pidls[n], &attribs);
1058
1059 if (SUCCEEDED(hr)) {
1060 if (attribs != (SFGAOF)~SFGAO_FILESYSTEM) {
1061 fill_w32fdata_shell(folder, pidls[n], attribs, &entry->data);
1062
1063 entry->bhfi_valid = TRUE;
1064 } else
1065 attribs = 0;
1066 } else
1067 attribs = 0;
1068
1069 entry->pidl = pidls[n];
1070
1071 if (entry->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
1072 hr = IShellFolder_BindToObject(folder, pidls[n], 0, &IID_IShellFolder, (void**)&child);
1073
1074 if (SUCCEEDED(hr))
1075 entry->folder = child;
1076 else
1077 entry->folder = NULL;
1078 }
1079 else
1080 entry->folder = NULL;
1081
1082 if (!entry->data.cFileName[0])
1083 /*hr = */name_from_pidl(folder, pidls[n], entry->data.cFileName, MAX_PATH, /*SHGDN_INFOLDER*/0x2000/*0x2000=SHGDN_INCLUDE_NONFILESYS*/);
1084
1085 /* get display icons for files and virtual objects */
1086 if (!(entry->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1087 !(attribs & SFGAO_FILESYSTEM)) {
1088 entry->hicon = extract_icon(folder, pidls[n]);
1089
1090 if (!entry->hicon)
1091 entry->hicon = (HICON)-1; /* don't try again later */
1092 }
1093
1094 entry->down = NULL;
1095 entry->up = dir;
1096 entry->expanded = FALSE;
1097 entry->scanned = FALSE;
1098 entry->level = level;
1099
1100 #ifndef _NO_EXTENSIONS
1101 entry->etype = ET_SHELL;
1102 entry->bhfi_valid = FALSE;
1103 #endif
1104
1105 last = entry;
1106 }
1107 }
1108
1109 IEnumIDList_Release(idlist);
1110 }
1111
1112 if (last)
1113 last->next = NULL;
1114
1115 dir->down = first_entry;
1116 dir->scanned = TRUE;
1117 }
1118
1119 #endif /* _SHELL_FOLDERS */
1120
1121
1122 /* sort order for different directory/file types */
1123 enum TYPE_ORDER {
1124 TO_DIR = 0,
1125 TO_DOT = 1,
1126 TO_DOTDOT = 2,
1127 TO_OTHER_DIR = 3,
1128 TO_FILE = 4
1129 };
1130
1131 /* distinguish between ".", ".." and any other directory names */
1132 static int TypeOrderFromDirname(LPCTSTR name)
1133 {
1134 if (name[0] == '.') {
1135 if (name[1] == '\0')
1136 return TO_DOT; /* "." */
1137
1138 if (name[1]=='.' && name[2]=='\0')
1139 return TO_DOTDOT; /* ".." */
1140 }
1141
1142 return TO_OTHER_DIR; /* anything else */
1143 }
1144
1145 /* directories first... */
1146 static int compareType(const WIN32_FIND_DATA* fd1, const WIN32_FIND_DATA* fd2)
1147 {
1148 int order1 = fd1->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY? TO_DIR: TO_FILE;
1149 int order2 = fd2->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY? TO_DIR: TO_FILE;
1150
1151 /* Handle "." and ".." as special case and move them at the very first beginning. */
1152 if (order1==TO_DIR && order2==TO_DIR) {
1153 order1 = TypeOrderFromDirname(fd1->cFileName);
1154 order2 = TypeOrderFromDirname(fd2->cFileName);
1155 }
1156
1157 return order2==order1? 0: order1<order2? -1: 1;
1158 }
1159
1160
1161 static int compareName(const void* arg1, const void* arg2)
1162 {
1163 const WIN32_FIND_DATA* fd1 = &(*(const Entry* const*)arg1)->data;
1164 const WIN32_FIND_DATA* fd2 = &(*(const Entry* const*)arg2)->data;
1165
1166 int cmp = compareType(fd1, fd2);
1167 if (cmp)
1168 return cmp;
1169
1170 return lstrcmpi(fd1->cFileName, fd2->cFileName);
1171 }
1172
1173 static int compareExt(const void* arg1, const void* arg2)
1174 {
1175 const WIN32_FIND_DATA* fd1 = &(*(const Entry* const*)arg1)->data;
1176 const WIN32_FIND_DATA* fd2 = &(*(const Entry* const*)arg2)->data;
1177 const TCHAR *name1, *name2, *ext1, *ext2;
1178
1179 int cmp = compareType(fd1, fd2);
1180 if (cmp)
1181 return cmp;
1182
1183 name1 = fd1->cFileName;
1184 name2 = fd2->cFileName;
1185
1186 ext1 = strrchrW(name1, '.');
1187 ext2 = strrchrW(name2, '.');
1188
1189 if (ext1)
1190 ext1++;
1191 else
1192 ext1 = sEmpty;
1193
1194 if (ext2)
1195 ext2++;
1196 else
1197 ext2 = sEmpty;
1198
1199 cmp = lstrcmpi(ext1, ext2);
1200 if (cmp)
1201 return cmp;
1202
1203 return lstrcmpi(name1, name2);
1204 }
1205
1206 static int compareSize(const void* arg1, const void* arg2)
1207 {
1208 const WIN32_FIND_DATA* fd1 = &(*(const Entry* const*)arg1)->data;
1209 const WIN32_FIND_DATA* fd2 = &(*(const Entry* const*)arg2)->data;
1210
1211 int cmp = compareType(fd1, fd2);
1212 if (cmp)
1213 return cmp;
1214
1215 cmp = fd2->nFileSizeHigh - fd1->nFileSizeHigh;
1216
1217 if (cmp < 0)
1218 return -1;
1219 else if (cmp > 0)
1220 return 1;
1221
1222 cmp = fd2->nFileSizeLow - fd1->nFileSizeLow;
1223
1224 return cmp<0? -1: cmp>0? 1: 0;
1225 }
1226
1227 static int compareDate(const void* arg1, const void* arg2)
1228 {
1229 const WIN32_FIND_DATA* fd1 = &(*(const Entry* const*)arg1)->data;
1230 const WIN32_FIND_DATA* fd2 = &(*(const Entry* const*)arg2)->data;
1231
1232 int cmp = compareType(fd1, fd2);
1233 if (cmp)
1234 return cmp;
1235
1236 return CompareFileTime(&fd2->ftLastWriteTime, &fd1->ftLastWriteTime);
1237 }
1238
1239
1240 static int (*sortFunctions[])(const void* arg1, const void* arg2) = {
1241 compareName, /* SORT_NAME */
1242 compareExt, /* SORT_EXT */
1243 compareSize, /* SORT_SIZE */
1244 compareDate /* SORT_DATE */
1245 };
1246
1247
1248 static void SortDirectory(Entry* dir, SORT_ORDER sortOrder)
1249 {
1250 Entry* entry = dir->down;
1251 Entry** array, **p;
1252 int len;
1253
1254 len = 0;
1255 for(entry=dir->down; entry; entry=entry->next)
1256 len++;
1257
1258 if (len) {
1259 array = HeapAlloc(GetProcessHeap(), 0, len*sizeof(Entry*));
1260
1261 p = array;
1262 for(entry=dir->down; entry; entry=entry->next)
1263 *p++ = entry;
1264
1265 /* call qsort with the appropriate compare function */
1266 qsort(array, len, sizeof(array[0]), sortFunctions[sortOrder]);
1267
1268 dir->down = array[0];
1269
1270 for(p=array; --len; p++)
1271 p[0]->next = p[1];
1272
1273 (*p)->next = 0;
1274
1275 HeapFree(GetProcessHeap(), 0, array);
1276 }
1277 }
1278
1279
1280 static void read_directory(Entry* dir, LPCTSTR path, SORT_ORDER sortOrder, HWND hwnd)
1281 {
1282 TCHAR buffer[MAX_PATH];
1283 Entry* entry;
1284 LPCTSTR s;
1285 PTSTR d;
1286
1287 #ifdef _SHELL_FOLDERS
1288 if (dir->etype == ET_SHELL)
1289 {
1290 read_directory_shell(dir, hwnd);
1291
1292 if (Globals.prescan_node) {
1293 s = path;
1294 d = buffer;
1295
1296 while(*s)
1297 *d++ = *s++;
1298
1299 *d++ = '\\';
1300
1301 for(entry=dir->down; entry; entry=entry->next)
1302 if (entry->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
1303 read_directory_shell(entry, hwnd);
1304 SortDirectory(entry, sortOrder);
1305 }
1306 }
1307 }
1308 else
1309 #endif
1310 #if !defined(_NO_EXTENSIONS) && defined(__WINE__)
1311 if (dir->etype == ET_UNIX)
1312 {
1313 read_directory_unix(dir, path);
1314
1315 if (Globals.prescan_node) {
1316 s = path;
1317 d = buffer;
1318
1319 while(*s)
1320 *d++ = *s++;
1321
1322 *d++ = '/';
1323
1324 for(entry=dir->down; entry; entry=entry->next)
1325 if (entry->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
1326 lstrcpy(d, entry->data.cFileName);
1327 read_directory_unix(entry, buffer);
1328 SortDirectory(entry, sortOrder);
1329 }
1330 }
1331 }
1332 else
1333 #endif
1334 {
1335 read_directory_win(dir, path);
1336
1337 if (Globals.prescan_node) {
1338 s = path;
1339 d = buffer;
1340
1341 while(*s)
1342 *d++ = *s++;
1343
1344 *d++ = '\\';
1345
1346 for(entry=dir->down; entry; entry=entry->next)
1347 if (entry->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
1348 lstrcpy(d, entry->data.cFileName);
1349 read_directory_win(entry, buffer);
1350 SortDirectory(entry, sortOrder);
1351 }
1352 }
1353 }
1354
1355 SortDirectory(dir, sortOrder);
1356 }
1357
1358
1359 static Entry* read_tree(Root* root, LPCTSTR path, LPITEMIDLIST pidl, LPTSTR drv, SORT_ORDER sortOrder, HWND hwnd)
1360 {
1361 #if !defined(_NO_EXTENSIONS) && defined(__WINE__)
1362 static const TCHAR sSlash[] = {'/', '\0'};
1363 #endif
1364 static const TCHAR sBackslash[] = {'\\', '\0'};
1365
1366 #ifdef _SHELL_FOLDERS
1367 if (pidl)
1368 {
1369 /* read shell namespace tree */
1370 root->drive_type = DRIVE_UNKNOWN;
1371 drv[0] = '\\';
1372 drv[1] = '\0';
1373 load_string(root->volname, sizeof(root->volname)/sizeof(root->volname[0]), IDS_DESKTOP);
1374 root->fs_flags = 0;
1375 load_string(root->fs, sizeof(root->fs)/sizeof(root->fs[0]), IDS_SHELL);
1376
1377 return read_tree_shell(root, pidl, sortOrder, hwnd);
1378 }
1379 else
1380 #endif
1381 #if !defined(_NO_EXTENSIONS) && defined(__WINE__)
1382 if (*path == '/')
1383 {
1384 /* read unix file system tree */
1385 root->drive_type = GetDriveType(path);
1386
1387 lstrcat(drv, sSlash);
1388 load_string(root->volname, sizeof(root->volname)/sizeof(root->volname[0]), IDS_ROOT_FS);
1389 root->fs_flags = 0;
1390 load_string(root->fs, sizeof(root->fs)/sizeof(root->fs[0]), IDS_UNIXFS);
1391
1392 lstrcpy(root->path, sSlash);
1393
1394 return read_tree_unix(root, path, sortOrder, hwnd);
1395 }
1396 #endif
1397
1398 /* read WIN32 file system tree */
1399 root->drive_type = GetDriveType(path);
1400
1401 lstrcat(drv, sBackslash);
1402 GetVolumeInformation(drv, root->volname, _MAX_FNAME, 0, 0, &root->fs_flags, root->fs, _MAX_DIR);
1403
1404 lstrcpy(root->path, drv);
1405
1406 return read_tree_win(root, path, sortOrder, hwnd);
1407 }
1408
1409
1410 /* flags to filter different file types */
1411 enum TYPE_FILTER {
1412 TF_DIRECTORIES = 0x01,
1413 TF_PROGRAMS = 0x02,
1414 TF_DOCUMENTS = 0x04,
1415 TF_OTHERS = 0x08,
1416 TF_HIDDEN = 0x10,
1417 TF_ALL = 0x1F
1418 };
1419
1420
1421 static ChildWnd* alloc_child_window(LPCTSTR path, LPITEMIDLIST pidl, HWND hwnd)
1422 {
1423 TCHAR drv[_MAX_DRIVE+1], dir[_MAX_DIR], name[_MAX_FNAME], ext[_MAX_EXT];
1424 TCHAR dir_path[MAX_PATH];
1425 TCHAR b1[BUFFER_LEN];
1426 static const TCHAR sAsterics[] = {'*', '\0'};
1427
1428 ChildWnd* child = HeapAlloc(GetProcessHeap(), 0, sizeof(ChildWnd));
1429 Root* root = &child->root;
1430 Entry* entry;
1431
1432 memset(child, 0, sizeof(ChildWnd));
1433
1434 child->left.treePane = TRUE;
1435 child->left.visible_cols = 0;
1436
1437 child->right.treePane = FALSE;
1438 #ifndef _NO_EXTENSIONS
1439 child->right.visible_cols = COL_SIZE|COL_DATE|COL_TIME|COL_ATTRIBUTES|COL_INDEX|COL_LINKS;
1440 #else
1441 child->right.visible_cols = COL_SIZE|COL_DATE|COL_TIME|COL_ATTRIBUTES;
1442 #endif
1443
1444 child->pos.length = sizeof(WINDOWPLACEMENT);
1445 child->pos.flags = 0;
1446 child->pos.showCmd = SW_SHOWNORMAL;
1447 child->pos.rcNormalPosition.left = CW_USEDEFAULT;
1448 child->pos.rcNormalPosition.top = CW_USEDEFAULT;
1449 child->pos.rcNormalPosition.right = CW_USEDEFAULT;
1450 child->pos.rcNormalPosition.bottom = CW_USEDEFAULT;
1451
1452 child->focus_pane = 0;
1453 child->split_pos = DEFAULT_SPLIT_POS;
1454 child->sortOrder = SORT_NAME;
1455 child->header_wdths_ok = FALSE;
1456
1457 if (path)
1458 {
1459 lstrcpy(child->path, path);
1460
1461 _tsplitpath(path, drv, dir, name, ext);
1462 }
1463
1464 lstrcpy(child->filter_pattern, sAsterics);
1465 child->filter_flags = TF_ALL;
1466
1467 root->entry.level = 0;
1468
1469 lstrcpy(dir_path, drv);
1470 lstrcat(dir_path, dir);
1471 entry = read_tree(root, dir_path, pidl, drv, child->sortOrder, hwnd);
1472
1473 #ifdef _SHELL_FOLDERS
1474 if (root->entry.etype == ET_SHELL)
1475 load_string(root->entry.data.cFileName, sizeof(root->entry.data.cFileName)/sizeof(root->entry.data.cFileName[0]), IDS_DESKTOP);
1476 else
1477 #endif
1478 wsprintf(root->entry.data.cFileName, RS(b1,IDS_TITLEFMT), drv, root->fs);
1479
1480 root->entry.data.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1481
1482 child->left.root = &root->entry;
1483 child->right.root = NULL;
1484
1485 set_curdir(child, entry, 0, hwnd);
1486
1487 return child;
1488 }
1489
1490
1491 /* free all memory associated with a child window */
1492 static void free_child_window(ChildWnd* child)
1493 {
1494 free_entries(&child->root.entry);
1495 HeapFree(GetProcessHeap(), 0, child);
1496 }
1497
1498
1499 /* get full path of specified directory entry */
1500 static void get_path(Entry* dir, PTSTR path)
1501 {
1502 Entry* entry;
1503 int len = 0;
1504 int level = 0;
1505
1506 #ifdef _SHELL_FOLDERS
1507 if (dir->etype == ET_SHELL)
1508 {
1509 SFGAOF attribs;
1510 HRESULT hr = S_OK;
1511
1512 path[0] = '\0';
1513
1514 attribs = 0;
1515
1516 if (dir->folder)
1517 hr = IShellFolder_GetAttributesOf(dir->folder, 1, (LPCITEMIDLIST*)&dir->pidl, &attribs);
1518
1519 if (SUCCEEDED(hr) && (attribs&SFGAO_FILESYSTEM)) {
1520 IShellFolder* parent = dir->up? dir->up->folder: Globals.iDesktop;
1521
1522 hr = path_from_pidl(parent, dir->pidl, path, MAX_PATH);
1523 }
1524 }
1525 else
1526 #endif
1527 {
1528 for(entry=dir; entry; level++) {
1529 LPCTSTR name;
1530 int l;
1531
1532 {
1533 LPCTSTR s;
1534 name = entry->data.cFileName;
1535 s = name;
1536
1537 for(l=0; *s && *s != '/' && *s != '\\'; s++)
1538 l++;
1539 }
1540
1541 if (entry->up) {
1542 if (l > 0) {
1543 memmove(path+l+1, path, len*sizeof(TCHAR));
1544 memcpy(path+1, name, l*sizeof(TCHAR));
1545 len += l+1;
1546
1547 #ifndef _NO_EXTENSIONS
1548 if (entry->etype == ET_UNIX)
1549 path[0] = '/';
1550 else
1551 #endif
1552 path[0] = '\\';
1553 }
1554
1555 entry = entry->up;
1556 } else {
1557 memmove(path+l, path, len*sizeof(TCHAR));
1558 memcpy(path, name, l*sizeof(TCHAR));
1559 len += l;
1560 break;
1561 }
1562 }
1563
1564 if (!level) {
1565 #ifndef _NO_EXTENSIONS
1566 if (entry->etype == ET_UNIX)
1567 path[len++] = '/';
1568 else
1569 #endif
1570 path[len++] = '\\';
1571 }
1572
1573 path[len] = '\0';
1574 }
1575 }
1576
1577 static windowOptions load_registry_settings(void)
1578 {
1579 DWORD size;
1580 DWORD type;
1581 HKEY hKey;
1582 windowOptions opts;
1583 LOGFONT logfont;
1584
1585 RegOpenKeyExW( HKEY_CURRENT_USER, registry_key,
1586 0, KEY_QUERY_VALUE, &hKey );
1587
1588 size = sizeof(DWORD);
1589
1590 if( RegQueryValueExW( hKey, reg_start_x, NULL, &type,
1591 (LPBYTE) &opts.start_x, &size ) != ERROR_SUCCESS )
1592 opts.start_x = CW_USEDEFAULT;
1593
1594 if( RegQueryValueExW( hKey, reg_start_y, NULL, &type,
1595 (LPBYTE) &opts.start_y, &size ) != ERROR_SUCCESS )
1596 opts.start_y = CW_USEDEFAULT;
1597
1598 if( RegQueryValueExW( hKey, reg_width, NULL, &type,
1599 (LPBYTE) &opts.width, &size ) != ERROR_SUCCESS )
1600 opts.width = CW_USEDEFAULT;
1601
1602 if( RegQueryValueExW( hKey, reg_height, NULL, &type,
1603 (LPBYTE) &opts.height, &size ) != ERROR_SUCCESS )
1604 opts.height = CW_USEDEFAULT;
1605 size=sizeof(logfont);
1606 if( RegQueryValueExW( hKey, reg_logfont, NULL, &type,
1607 (LPBYTE) &logfont, &size ) != ERROR_SUCCESS )
1608 GetObject(GetStockObject(DEFAULT_GUI_FONT),sizeof(logfont),&logfont);
1609
1610 RegCloseKey( hKey );
1611
1612 Globals.hfont = CreateFontIndirect(&logfont);
1613 return opts;
1614 }
1615
1616 static void save_registry_settings(void)
1617 {
1618 WINDOWINFO wi;
1619 HKEY hKey;
1620 INT width, height;
1621 LOGFONT logfont;
1622
1623 wi.cbSize = sizeof( WINDOWINFO );
1624 GetWindowInfo(Globals.hMainWnd, &wi);
1625 width = wi.rcWindow.right - wi.rcWindow.left;
1626 height = wi.rcWindow.bottom - wi.rcWindow.top;
1627
1628 if ( RegOpenKeyExW( HKEY_CURRENT_USER, registry_key,
1629 0, KEY_SET_VALUE, &hKey ) != ERROR_SUCCESS )
1630 {
1631 /* Unable to save registry settings - try to create key */
1632 if ( RegCreateKeyExW( HKEY_CURRENT_USER, registry_key,
1633 0, NULL, REG_OPTION_NON_VOLATILE,
1634 KEY_SET_VALUE, NULL, &hKey, NULL ) != ERROR_SUCCESS )
1635 {
1636 /* FIXME: Cannot create key */
1637 return;
1638 }
1639 }
1640 /* Save all of the settings */
1641 RegSetValueExW( hKey, reg_start_x, 0, REG_DWORD,
1642 (LPBYTE) &wi.rcWindow.left, sizeof(DWORD) );
1643 RegSetValueExW( hKey, reg_start_y, 0, REG_DWORD,
1644 (LPBYTE) &wi.rcWindow.top, sizeof(DWORD) );
1645 RegSetValueExW( hKey, reg_width, 0, REG_DWORD,
1646 (LPBYTE) &width, sizeof(DWORD) );
1647 RegSetValueExW( hKey, reg_height, 0, REG_DWORD,
1648 (LPBYTE) &height, sizeof(DWORD) );
1649 GetObject(Globals.hfont, sizeof(logfont), &logfont);
1650 RegSetValueExW( hKey, reg_logfont, 0, REG_BINARY,
1651 (LPBYTE) &logfont, sizeof(LOGFONT) );
1652
1653 /* TODO: Save more settings here (List vs. Detailed View, etc.) */
1654 RegCloseKey( hKey );
1655 }
1656
1657 static void resize_frame_rect(HWND hwnd, PRECT prect)
1658 {
1659 int new_top;
1660 RECT rt;
1661
1662 if (IsWindowVisible(Globals.htoolbar)) {
1663 SendMessage(Globals.htoolbar, WM_SIZE, 0, 0);
1664 GetClientRect(Globals.htoolbar, &rt);
1665 prect->top = rt.bottom+3;
1666 prect->bottom -= rt.bottom+3;
1667 }
1668
1669 if (IsWindowVisible(Globals.hdrivebar)) {
1670 SendMessage(Globals.hdrivebar, WM_SIZE, 0, 0);
1671 GetClientRect(Globals.hdrivebar, &rt);
1672 new_top = --prect->top + rt.bottom+3;
1673 MoveWindow(Globals.hdrivebar, 0, prect->top, rt.right, new_top, TRUE);
1674 prect->top = new_top;
1675 prect->bottom -= rt.bottom+2;
1676 }
1677
1678 if (IsWindowVisible(Globals.hstatusbar)) {
1679 int parts[] = {300, 500};
1680
1681 SendMessage(Globals.hstatusbar, WM_SIZE, 0, 0);
1682 SendMessage(Globals.hstatusbar, SB_SETPARTS, 2, (LPARAM)&parts);
1683 GetClientRect(Globals.hstatusbar, &rt);
1684 prect->bottom -= rt.bottom;
1685 }
1686
1687 MoveWindow(Globals.hmdiclient, prect->left-1,prect->top-1,prect->right+2,prect->bottom+1, TRUE);
1688 }
1689
1690 static void resize_frame(HWND hwnd, int cx, int cy)
1691 {
1692 RECT rect;
1693
1694 rect.left = 0;
1695 rect.top = 0;
1696 rect.right = cx;
1697 rect.bottom = cy;
1698
1699 resize_frame_rect(hwnd, &rect);
1700 }
1701
1702 static void resize_frame_client(HWND hwnd)
1703 {
1704 RECT rect;
1705
1706 GetClientRect(hwnd, &rect);
1707
1708 resize_frame_rect(hwnd, &rect);
1709 }
1710
1711
1712 static HHOOK hcbthook;
1713 static ChildWnd* newchild = NULL;
1714
1715 static LRESULT CALLBACK CBTProc(int code, WPARAM wparam, LPARAM lparam)
1716 {
1717 if (code==HCBT_CREATEWND && newchild) {
1718 ChildWnd* child = newchild;
1719 newchild = NULL;
1720
1721 child->hwnd = (HWND) wparam;
1722 SetWindowLongPtr(child->hwnd, GWLP_USERDATA, (LPARAM)child);
1723 }
1724
1725 return CallNextHookEx(hcbthook, code, wparam, lparam);
1726 }
1727
1728 static HWND create_child_window(ChildWnd* child)
1729 {
1730 MDICREATESTRUCT mcs;
1731 int idx;
1732
1733 mcs.szClass = sWINEFILETREE;
1734 mcs.szTitle = (LPTSTR)child->path;
1735 mcs.hOwner = Globals.hInstance;
1736 mcs.x = child->pos.rcNormalPosition.left;
1737 mcs.y = child->pos.rcNormalPosition.top;
1738 mcs.cx = child->pos.rcNormalPosition.right-child->pos.rcNormalPosition.left;
1739 mcs.cy = child->pos.rcNormalPosition.bottom-child->pos.rcNormalPosition.top;
1740 mcs.style = 0;
1741 mcs.lParam = 0;
1742
1743 hcbthook = SetWindowsHookEx(WH_CBT, CBTProc, 0, GetCurrentThreadId());
1744
1745 newchild = child;
1746 child->hwnd = (HWND) SendMessage(Globals.hmdiclient, WM_MDICREATE, 0, (LPARAM)&mcs);
1747 if (!child->hwnd) {
1748 UnhookWindowsHookEx(hcbthook);
1749 return 0;
1750 }
1751
1752 UnhookWindowsHookEx(hcbthook);
1753
1754 SendMessage(child->left.hwnd, LB_SETITEMHEIGHT, 1, max(Globals.spaceSize.cy,IMAGE_HEIGHT+3));
1755 SendMessage(child->right.hwnd, LB_SETITEMHEIGHT, 1, max(Globals.spaceSize.cy,IMAGE_HEIGHT+3));
1756
1757 idx = SendMessage(child->left.hwnd, LB_FINDSTRING, 0, (LPARAM)child->left.cur);
1758 SendMessage(child->left.hwnd, LB_SETCURSEL, idx, 0);
1759
1760 return child->hwnd;
1761 }
1762
1763
1764 struct ExecuteDialog {
1765 TCHAR cmd[MAX_PATH];
1766 int cmdshow;
1767 };
1768
1769 static INT_PTR CALLBACK ExecuteDialogDlgProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam)
1770 {
1771 static struct ExecuteDialog* dlg;
1772
1773 switch(nmsg) {
1774 case WM_INITDIALOG:
1775 dlg = (struct ExecuteDialog*) lparam;
1776 return 1;
1777
1778 case WM_COMMAND: {
1779 int id = (int)wparam;
1780
1781 if (id == IDOK) {
1782 GetWindowText(GetDlgItem(hwnd, 201), dlg->cmd, MAX_PATH);
1783 dlg->cmdshow = get_check(hwnd,214) ? SW_SHOWMINIMIZED : SW_SHOWNORMAL;
1784 EndDialog(hwnd, id);
1785 } else if (id == IDCANCEL)
1786 EndDialog(hwnd, id);
1787
1788 return 1;}
1789 }
1790
1791 return 0;
1792 }
1793
1794
1795 static INT_PTR CALLBACK DestinationDlgProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam)
1796 {
1797 TCHAR b1[BUFFER_LEN], b2[BUFFER_LEN];
1798
1799 switch(nmsg) {
1800 case WM_INITDIALOG:
1801 SetWindowLongPtr(hwnd, GWLP_USERDATA, lparam);
1802 SetWindowText(GetDlgItem(hwnd, 201), (LPCTSTR)lparam);
1803 return 1;
1804
1805 case WM_COMMAND: {
1806 int id = (int)wparam;
1807
1808 switch(id) {
1809 case IDOK: {
1810 LPTSTR dest = (LPTSTR) GetWindowLongPtr(hwnd, GWLP_USERDATA);
1811 GetWindowText(GetDlgItem(hwnd, 201), dest, MAX_PATH);
1812 EndDialog(hwnd, id);
1813 break;}
1814
1815 case IDCANCEL:
1816 EndDialog(hwnd, id);
1817 break;
1818
1819 case 254:
1820 MessageBox(hwnd, RS(b1,IDS_NO_IMPL), RS(b2,IDS_WINEFILE), MB_OK);
1821 break;
1822 }
1823
1824 return 1;
1825 }
1826 }
1827
1828 return 0;
1829 }
1830
1831
1832 struct FilterDialog {
1833 TCHAR pattern[MAX_PATH];
1834 int flags;
1835 };
1836
1837 static INT_PTR CALLBACK FilterDialogDlgProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam)
1838 {
1839 static struct FilterDialog* dlg;
1840
1841 switch(nmsg) {
1842 case WM_INITDIALOG:
1843 dlg = (struct FilterDialog*) lparam;
1844 SetWindowText(GetDlgItem(hwnd, IDC_VIEW_PATTERN), dlg->pattern);
1845 set_check(hwnd, IDC_VIEW_TYPE_DIRECTORIES, dlg->flags&TF_DIRECTORIES);
1846 set_check(hwnd, IDC_VIEW_TYPE_PROGRAMS, dlg->flags&TF_PROGRAMS);
1847 set_check(hwnd, IDC_VIEW_TYPE_DOCUMENTS, dlg->flags&TF_DOCUMENTS);
1848 set_check(hwnd, IDC_VIEW_TYPE_OTHERS, dlg->flags&TF_OTHERS);
1849 set_check(hwnd, IDC_VIEW_TYPE_HIDDEN, dlg->flags&TF_HIDDEN);
1850 return 1;
1851
1852 case WM_COMMAND: {
1853 int id = (int)wparam;
1854
1855 if (id == IDOK) {
1856 int flags = 0;
1857
1858 GetWindowText(GetDlgItem(hwnd, IDC_VIEW_PATTERN), dlg->pattern, MAX_PATH);
1859
1860 flags |= get_check(hwnd, IDC_VIEW_TYPE_DIRECTORIES) ? TF_DIRECTORIES : 0;
1861 flags |= get_check(hwnd, IDC_VIEW_TYPE_PROGRAMS) ? TF_PROGRAMS : 0;
1862 flags |= get_check(hwnd, IDC_VIEW_TYPE_DOCUMENTS) ? TF_DOCUMENTS : 0;
1863 flags |= get_check(hwnd, IDC_VIEW_TYPE_OTHERS) ? TF_OTHERS : 0;
1864 flags |= get_check(hwnd, IDC_VIEW_TYPE_HIDDEN) ? TF_HIDDEN : 0;
1865
1866 dlg->flags = flags;
1867
1868 EndDialog(hwnd, id);
1869 } else if (id == IDCANCEL)
1870 EndDialog(hwnd, id);
1871
1872 return 1;}
1873 }
1874
1875 return 0;
1876 }
1877
1878
1879 struct PropertiesDialog {
1880 TCHAR path[MAX_PATH];
1881 Entry entry;
1882 void* pVersionData;
1883 };
1884
1885 /* Structure used to store enumerated languages and code pages. */
1886 struct LANGANDCODEPAGE {
1887 WORD wLanguage;
1888 WORD wCodePage;
1889 } *lpTranslate;
1890
1891 static LPCSTR InfoStrings[] = {
1892 "Comments",
1893 "CompanyName",
1894 "FileDescription",
1895 "FileVersion",
1896 "InternalName",
1897 "LegalCopyright",
1898 "LegalTrademarks",
1899 "OriginalFilename",
1900 "PrivateBuild",
1901 "ProductName",
1902 "ProductVersion",
1903 "SpecialBuild",
1904 NULL
1905 };
1906
1907 static void PropDlg_DisplayValue(HWND hlbox, HWND hedit)
1908 {
1909 int idx = SendMessage(hlbox, LB_GETCURSEL, 0, 0);
1910
1911 if (idx != LB_ERR) {
1912 LPCTSTR pValue = (LPCTSTR) SendMessage(hlbox, LB_GETITEMDATA, idx, 0);
1913
1914 if (pValue)
1915 SetWindowText(hedit, pValue);
1916 }
1917 }
1918
1919 static void CheckForFileInfo(struct PropertiesDialog* dlg, HWND hwnd, LPCTSTR strFilename)
1920 {
1921 static TCHAR sBackSlash[] = {'\\','\0'};
1922 static TCHAR sTranslation[] = {'\\','V','a','r','F','i','l','e','I','n','f','o','\\','T','r','a','n','s','l','a','t','i','o','n','\0'};
1923 static TCHAR sStringFileInfo[] = {'\\','S','t','r','i','n','g','F','i','l','e','I','n','f','o','\\',
1924 '%','0','4','x','%','0','4','x','\\','%','s','\0'};
1925 DWORD dwVersionDataLen = GetFileVersionInfoSize(strFilename, NULL);
1926
1927 if (dwVersionDataLen) {
1928 dlg->pVersionData = HeapAlloc(GetProcessHeap(), 0, dwVersionDataLen);
1929
1930 if (GetFileVersionInfo(strFilename, 0, dwVersionDataLen, dlg->pVersionData)) {
1931 LPVOID pVal;
1932 UINT nValLen;
1933
1934 if (VerQueryValue(dlg->pVersionData, sBackSlash, &pVal, &nValLen)) {
1935 if (nValLen == sizeof(VS_FIXEDFILEINFO)) {
1936 VS_FIXEDFILEINFO* pFixedFileInfo = (VS_FIXEDFILEINFO*)pVal;
1937 char buffer[BUFFER_LEN];
1938
1939 sprintf(buffer, "%d.%d.%d.%d",
1940 HIWORD(pFixedFileInfo->dwFileVersionMS), LOWORD(pFixedFileInfo->dwFileVersionMS),
1941 HIWORD(pFixedFileInfo->dwFileVersionLS), LOWORD(pFixedFileInfo->dwFileVersionLS));
1942
1943 SetDlgItemTextA(hwnd, IDC_STATIC_PROP_VERSION, buffer);
1944 }
1945 }
1946
1947 /* Read the list of languages and code pages. */
1948 if (VerQueryValue(dlg->pVersionData, sTranslation, &pVal, &nValLen)) {
1949 struct LANGANDCODEPAGE* pTranslate = (struct LANGANDCODEPAGE*)pVal;
1950 struct LANGANDCODEPAGE* pEnd = (struct LANGANDCODEPAGE*)((LPBYTE)pVal+nValLen);
1951
1952 HWND hlbox = GetDlgItem(hwnd, IDC_LIST_PROP_VERSION_TYPES);
1953
1954 /* Read the file description for each language and code page. */
1955 for(; pTranslate<pEnd; ++pTranslate) {
1956 LPCSTR* p;
1957
1958 for(p=InfoStrings; *p; ++p) {
1959 TCHAR subblock[200];
1960 #ifdef UNICODE
1961 TCHAR infoStr[100];
1962 #endif
1963 LPCTSTR pTxt;
1964 UINT nValLen;
1965
1966 LPCSTR pInfoString = *p;
1967 #ifdef UNICODE
1968 MultiByteToWideChar(CP_ACP, 0, pInfoString, -1, infoStr, 100);
1969 #else
1970 #define infoStr pInfoString
1971 #endif
1972 wsprintf(subblock, sStringFileInfo, pTranslate->wLanguage, pTranslate->wCodePage, infoStr);
1973
1974 /* Retrieve file description for language and code page */
1975 if (VerQueryValue(dlg->pVersionData, subblock, (PVOID)&pTxt, &nValLen)) {
1976 int idx = SendMessage(hlbox, LB_ADDSTRING, 0L, (LPARAM)infoStr);
1977 SendMessage(hlbox, LB_SETITEMDATA, idx, (LPARAM) pTxt);
1978 }
1979 }
1980 }
1981
1982 SendMessage(hlbox, LB_SETCURSEL, 0, 0);
1983
1984 PropDlg_DisplayValue(hlbox, GetDlgItem(hwnd,IDC_LIST_PROP_VERSION_VALUES));
1985 }
1986 }
1987 }
1988 }
1989
1990 static INT_PTR CALLBACK PropertiesDialogDlgProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam)
1991 {
1992 static struct PropertiesDialog* dlg;
1993
1994 switch(nmsg) {
1995 case WM_INITDIALOG: {
1996 static const TCHAR sByteFmt[] = {'%','s',' ','B','y','t','e','s','\0'};
1997 TCHAR b1[BUFFER_LEN], b2[BUFFER_LEN];
1998 LPWIN32_FIND_DATA pWFD;
1999
2000 dlg = (struct PropertiesDialog*) lparam;
2001 pWFD = (LPWIN32_FIND_DATA) &dlg->entry.data;
2002
2003 GetWindowText(hwnd, b1, MAX_PATH);
2004 wsprintf(b2, b1, pWFD->cFileName);
2005 SetWindowText(hwnd, b2);
2006
2007 format_date(&pWFD->ftLastWriteTime, b1, COL_DATE|COL_TIME);
2008 SetWindowText(GetDlgItem(hwnd, IDC_STATIC_PROP_LASTCHANGE), b1);
2009
2010 format_longlong( b1, ((ULONGLONG)pWFD->nFileSizeHigh << 32) | pWFD->nFileSizeLow );
2011 wsprintf(b2, sByteFmt, b1);
2012 SetWindowText(GetDlgItem(hwnd, IDC_STATIC_PROP_SIZE), b2);
2013
2014 SetWindowText(GetDlgItem(hwnd, IDC_STATIC_PROP_FILENAME), pWFD->cFileName);
2015 SetWindowText(GetDlgItem(hwnd, IDC_STATIC_PROP_PATH), dlg->path);
2016
2017 set_check(hwnd, IDC_CHECK_READONLY, pWFD->dwFileAttributes&FILE_ATTRIBUTE_READONLY);
2018 set_check(hwnd, IDC_CHECK_ARCHIVE, pWFD->dwFileAttributes&FILE_ATTRIBUTE_ARCHIVE);
2019 set_check(hwnd, IDC_CHECK_COMPRESSED, pWFD->dwFileAttributes&FILE_ATTRIBUTE_COMPRESSED);
2020 set_check(hwnd, IDC_CHECK_HIDDEN, pWFD->dwFileAttributes&FILE_ATTRIBUTE_HIDDEN);
2021 set_check(hwnd, IDC_CHECK_SYSTEM, pWFD->dwFileAttributes&FILE_ATTRIBUTE_SYSTEM);
2022
2023 CheckForFileInfo(dlg, hwnd, dlg->path);
2024 return 1;}
2025
2026 case WM_COMMAND: {
2027 int id = (int)wparam;
2028
2029 switch(HIWORD(wparam)) {
2030 case LBN_SELCHANGE: {
2031 HWND hlbox = GetDlgItem(hwnd, IDC_LIST_PROP_VERSION_TYPES);
2032 PropDlg_DisplayValue(hlbox, GetDlgItem(hwnd,IDC_LIST_PROP_VERSION_VALUES));
2033 break;
2034 }
2035
2036 case BN_CLICKED:
2037 if (id==IDOK || id==IDCANCEL)
2038 EndDialog(hwnd, id);
2039 }
2040
2041 return 1;}
2042
2043 case WM_NCDESTROY:
2044 HeapFree(GetProcessHeap(), 0, dlg->pVersionData);
2045 dlg->pVersionData = NULL;
2046 break;
2047 }
2048
2049 return 0;
2050 }
2051
2052 static void show_properties_dlg(Entry* entry, HWND hwnd)
2053 {
2054 struct PropertiesDialog dlg;
2055
2056 memset(&dlg, 0, sizeof(struct PropertiesDialog));
2057 get_path(entry, dlg.path);
2058 memcpy(&dlg.entry, entry, sizeof(Entry));
2059
2060 DialogBoxParam(Globals.hInstance, MAKEINTRESOURCE(IDD_DIALOG_PROPERTIES), hwnd, PropertiesDialogDlgProc, (LPARAM)&dlg);
2061 }
2062
2063
2064 #ifndef _NO_EXTENSIONS
2065
2066 static struct FullScreenParameters {
2067 BOOL mode;
2068 RECT orgPos;
2069 BOOL wasZoomed;
2070 } g_fullscreen = {
2071 FALSE, /* mode */
2072 {0, 0, 0, 0},
2073 FALSE
2074 };
2075
2076 static void frame_get_clientspace(HWND hwnd, PRECT prect)
2077 {
2078 RECT rt;
2079
2080 if (!IsIconic(hwnd))
2081 GetClientRect(hwnd, prect);
2082 else {
2083 WINDOWPLACEMENT wp;
2084
2085 GetWindowPlacement(hwnd, &wp);
2086
2087 prect->left = prect->top = 0;
2088 prect->right = wp.rcNormalPosition.right-wp.rcNormalPosition.left-
2089 2*(GetSystemMetrics(SM_CXSIZEFRAME)+GetSystemMetrics(SM_CXEDGE));
2090 prect->bottom = wp.rcNormalPosition.bottom-wp.rcNormalPosition.top-
2091 2*(GetSystemMetrics(SM_CYSIZEFRAME)+GetSystemMetrics(SM_CYEDGE))-
2092 GetSystemMetrics(SM_CYCAPTION)-GetSystemMetrics(SM_CYMENUSIZE);
2093 }
2094
2095 if (IsWindowVisible(Globals.htoolbar)) {
2096 GetClientRect(Globals.htoolbar, &rt);
2097 prect->top += rt.bottom+2;
2098 }
2099
2100 if (IsWindowVisible(Globals.hdrivebar)) {
2101 GetClientRect(Globals.hdrivebar, &rt);
2102 prect->top += rt.bottom+2;
2103 }
2104
2105 if (IsWindowVisible(Globals.hstatusbar)) {
2106 GetClientRect(Globals.hstatusbar, &rt);
2107 prect->bottom -= rt.bottom;
2108 }
2109 }
2110
2111 static BOOL toggle_fullscreen(HWND hwnd)
2112 {
2113 RECT rt;
2114
2115 if ((g_fullscreen.mode=!g_fullscreen.mode)) {
2116 GetWindowRect(hwnd, &g_fullscreen.orgPos);
2117 g_fullscreen.wasZoomed = IsZoomed(hwnd);
2118
2119 Frame_CalcFrameClient(hwnd, &rt);
2120 ClientToScreen(hwnd, (LPPOINT)&rt.left);
2121 ClientToScreen(hwnd, (LPPOINT)&rt.right);
2122
2123 rt.left = g_fullscreen.orgPos.left-rt.left;
2124 rt.top = g_fullscreen.orgPos.top-rt.top;
2125 rt.right = GetSystemMetrics(SM_CXSCREEN)+g_fullscreen.orgPos.right-rt.right;
2126 rt.bottom = GetSystemMetrics(SM_CYSCREEN)+g_fullscreen.orgPos.bottom-rt.bottom;
2127
2128 MoveWindow(hwnd, rt.left, rt.top, rt.right-rt.left, rt.bottom-rt.top, TRUE);
2129 } else {
2130 MoveWindow(hwnd, g_fullscreen.orgPos.left, g_fullscreen.orgPos.top,
2131 g_fullscreen.orgPos.right-g_fullscreen.orgPos.left,
2132 g_fullscreen.orgPos.bottom-g_fullscreen.orgPos.top, TRUE);
2133
2134 if (g_fullscreen.wasZoomed)
2135 ShowWindow(hwnd, WS_MAXIMIZE);
2136 }
2137
2138 return g_fullscreen.mode;
2139 }
2140
2141 static void fullscreen_move(HWND hwnd)
2142 {
2143 RECT rt, pos;
2144 GetWindowRect(hwnd, &pos);
2145
2146 Frame_CalcFrameClient(hwnd, &rt);
2147 ClientToScreen(hwnd, (LPPOINT)&rt.left);
2148 ClientToScreen(hwnd, (LPPOINT)&rt.right);
2149
2150 rt.left = pos.left-rt.left;
2151 rt.top = pos.top-rt.top;
2152 rt.right = GetSystemMetrics(SM_CXSCREEN)+pos.right-rt.right;
2153 rt.bottom = GetSystemMetrics(SM_CYSCREEN)+pos.bottom-rt.bottom;
2154
2155 MoveWindow(hwnd, rt.left, rt.top, rt.right-rt.left, rt.bottom-rt.top, TRUE);
2156 }
2157
2158 #endif
2159
2160
2161 static void toggle_child(HWND hwnd, UINT cmd, HWND hchild)
2162 {
2163 BOOL vis = IsWindowVisible(hchild);
2164
2165 CheckMenuItem(Globals.hMenuOptions, cmd, vis?MF_BYCOMMAND:MF_BYCOMMAND|MF_CHECKED);
2166
2167 ShowWindow(hchild, vis?SW_HIDE:SW_SHOW);
2168
2169 #ifndef _NO_EXTENSIONS
2170 if (g_fullscreen.mode)
2171 fullscreen_move(hwnd);
2172 #endif
2173
2174 resize_frame_client(hwnd);
2175 }
2176
2177 static BOOL activate_drive_window(LPCTSTR path)
2178 {
2179 TCHAR drv1[_MAX_DRIVE], drv2[_MAX_DRIVE];
2180 HWND child_wnd;
2181
2182 _tsplitpath(path, drv1, 0, 0, 0);
2183
2184 /* search for a already open window for the same drive */
2185 for(child_wnd=GetNextWindow(Globals.hmdiclient,GW_CHILD); child_wnd; child_wnd=GetNextWindow(child_wnd, GW_HWNDNEXT)) {
2186 ChildWnd* child = (ChildWnd*) GetWindowLongPtr(child_wnd, GWLP_USERDATA);
2187
2188 if (child) {
2189 _tsplitpath(child->root.path, drv2, 0, 0, 0);
2190
2191 if (!lstrcmpi(drv2, drv1)) {
2192 SendMessage(Globals.hmdiclient, WM_MDIACTIVATE, (WPARAM)child_wnd, 0);
2193
2194 if (IsIconic(child_wnd))
2195 ShowWindow(child_wnd, SW_SHOWNORMAL);
2196
2197 return TRUE;
2198 }
2199 }
2200 }
2201
2202 return FALSE;
2203 }
2204
2205 static BOOL activate_fs_window(LPCTSTR filesys)
2206 {
2207 HWND child_wnd;
2208
2209 /* search for a already open window of the given file system name */
2210 for(child_wnd=GetNextWindow(Globals.hmdiclient,GW_CHILD); child_wnd; child_wnd=GetNextWindow(child_wnd, GW_HWNDNEXT)) {
2211 ChildWnd* child = (ChildWnd*) GetWindowLongPtr(child_wnd, GWLP_USERDATA);
2212
2213 if (child) {
2214 if (!lstrcmpi(child->root.fs, filesys)) {
2215 SendMessage(Globals.hmdiclient, WM_MDIACTIVATE, (WPARAM)child_wnd, 0);
2216
2217 if (IsIconic(child_wnd))
2218 ShowWindow(child_wnd, SW_SHOWNORMAL);
2219
2220 return TRUE;
2221 }
2222 }
2223 }
2224
2225 return FALSE;
2226 }
2227
2228 static LRESULT CALLBACK FrameWndProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam)
2229 {
2230 TCHAR b1[BUFFER_LEN], b2[BUFFER_LEN];
2231
2232 switch(nmsg) {
2233 case WM_CLOSE:
2234 if (Globals.saveSettings)
2235 save_registry_settings();
2236
2237 DestroyWindow(hwnd);
2238
2239 /* clear handle variables */
2240 Globals.hMenuFrame = 0;
2241 Globals.hMenuView = 0;
2242 Globals.hMenuOptions = 0;
2243 Globals.hMainWnd = 0;
2244 Globals.hmdiclient = 0;
2245 Globals.hdrivebar = 0;
2246 break;
2247
2248 case WM_DESTROY:
2249 PostQuitMessage(0);
2250 break;
2251
2252 case WM_INITMENUPOPUP: {
2253 HWND hwndClient = (HWND) SendMessage(Globals.hmdiclient, WM_MDIGETACTIVE, 0, 0);
2254
2255 if (!SendMessage(hwndClient, WM_INITMENUPOPUP, wparam, lparam))
2256 return 0;
2257 break;}
2258
2259 case WM_COMMAND: {
2260 UINT cmd = LOWORD(wparam);
2261 HWND hwndClient = (HWND) SendMessage(Globals.hmdiclient, WM_MDIGETACTIVE, 0, 0);
2262
2263 if (SendMessage(hwndClient, WM_DISPATCH_COMMAND, wparam, lparam))
2264 break;
2265
2266 if (cmd>=ID_DRIVE_FIRST && cmd<=ID_DRIVE_FIRST+0xFF) {
2267 TCHAR drv[_MAX_DRIVE], path[MAX_PATH];
2268 ChildWnd* child;
2269 LPCTSTR root = Globals.drives;
2270 int i;
2271
2272 for(i=cmd-ID_DRIVE_FIRST; i--; root++)
2273 while(*root)
2274 root++;
2275
2276 if (activate_drive_window(root))
2277 return 0;
2278
2279 _tsplitpath(root, drv, 0, 0, 0);
2280
2281 if (!SetCurrentDirectory(drv)) {
2282 display_error(hwnd, GetLastError());
2283 return 0;
2284 }
2285
2286 GetCurrentDirectory(MAX_PATH, path); /*TODO: store last directory per drive */
2287 child = alloc_child_window(path, NULL, hwnd);
2288
2289 if (!create_child_window(child))
2290 HeapFree(GetProcessHeap(), 0, child);
2291 } else switch(cmd) {
2292 case ID_FILE_EXIT:
2293 SendMessage(hwnd, WM_CLOSE, 0, 0);
2294 break;
2295
2296 case ID_WINDOW_NEW: {
2297 TCHAR path[MAX_PATH];
2298 ChildWnd* child;
2299
2300 GetCurrentDirectory(MAX_PATH, path);
2301 child = alloc_child_window(path, NULL, hwnd);
2302
2303 if (!create_child_window(child))
2304 HeapFree(GetProcessHeap(), 0, child);
2305 break;}
2306
2307 case ID_REFRESH:
2308 refresh_drives();
2309 break;
2310
2311 case ID_WINDOW_CASCADE:
2312 SendMessage(Globals.hmdiclient, WM_MDICASCADE, 0, 0);
2313 break;
2314
2315 case ID_WINDOW_TILE_HORZ:
2316 SendMessage(Globals.hmdiclient, WM_MDITILE, MDITILE_HORIZONTAL, 0);
2317 break;
2318
2319 case ID_WINDOW_TILE_VERT:
2320 SendMessage(Globals.hmdiclient, WM_MDITILE, MDITILE_VERTICAL, 0);
2321 break;
2322
2323 case ID_WINDOW_ARRANGE:
2324 SendMessage(Globals.hmdiclient, WM_MDIICONARRANGE, 0, 0);
2325 break;
2326
2327 case ID_SELECT_FONT:
2328 choose_font(hwnd);
2329 break;
2330
2331 case ID_VIEW_TOOL_BAR:
2332 toggle_child(hwnd, cmd, Globals.htoolbar);
2333 break;
2334
2335 case ID_VIEW_DRIVE_BAR:
2336 toggle_child(hwnd, cmd, Globals.hdrivebar);
2337 break;
2338
2339 case ID_VIEW_STATUSBAR:
2340 toggle_child(hwnd, cmd, Globals.hstatusbar);
2341 break;
2342
2343 case ID_VIEW_SAVESETTINGS:
2344 Globals.saveSettings = !Globals.saveSettings;
2345 CheckMenuItem(Globals.hMenuOptions, ID_VIEW_SAVESETTINGS,
2346 Globals.saveSettings ? MF_CHECKED : MF_UNCHECKED );
2347 break;
2348
2349 case ID_EXECUTE: {
2350 struct ExecuteDialog dlg;
2351
2352 memset(&dlg, 0, sizeof(struct ExecuteDialog));
2353
2354 if (DialogBoxParam(Globals.hInstance, MAKEINTRESOURCE(IDD_EXECUTE), hwnd, ExecuteDialogDlgProc, (LPARAM)&dlg) == IDOK) {
2355 HINSTANCE hinst = ShellExecute(hwnd, NULL/*operation*/, dlg.cmd/*file*/, NULL/*parameters*/, NULL/*dir*/, dlg.cmdshow);
2356
2357 if (PtrToUlong(hinst) <= 32)
2358 display_error(hwnd, GetLastError());
2359 }
2360 break;}
2361
2362 case ID_CONNECT_NETWORK_DRIVE: {
2363 DWORD ret = WNetConnectionDialog(hwnd, RESOURCETYPE_DISK);
2364 if (ret == NO_ERROR)
2365 refresh_drives();
2366 else if (ret != (DWORD)-1) {
2367 if (ret == ERROR_EXTENDED_ERROR)
2368 display_network_error(hwnd);
2369 else
2370 display_error(hwnd, ret);
2371 }
2372 break;}
2373
2374 case ID_DISCONNECT_NETWORK_DRIVE: {
2375 DWORD ret = WNetDisconnectDialog(hwnd, RESOURCETYPE_DISK);
2376 if (ret == NO_ERROR)
2377 refresh_drives();
2378 else if (ret != (DWORD)-1) {
2379 if (ret == ERROR_EXTENDED_ERROR)
2380 display_network_error(hwnd);
2381 else
2382 display_error(hwnd, ret);
2383 }
2384 break;}
2385
2386 case ID_FORMAT_DISK: {
2387 UINT sem_org = SetErrorMode(0); /* Get the current Error Mode settings. */
2388 SetErrorMode(sem_org & ~SEM_FAILCRITICALERRORS); /* Force O/S to handle */
2389 SHFormatDrive(hwnd, 0 /* A: */, SHFMT_ID_DEFAULT, 0);
2390 SetErrorMode(sem_org); /* Put it back the way it was. */
2391 break;}
2392
2393 case ID_HELP:
2394 WinHelp(hwnd, RS(b1,IDS_WINEFILE), HELP_INDEX, 0);
2395 break;
2396
2397 #ifndef _NO_EXTENSIONS
2398 case ID_VIEW_FULLSCREEN:
2399 CheckMenuItem(Globals.hMenuOptions, cmd, toggle_fullscreen(hwnd)?MF_CHECKED:0);
2400 break;
2401
2402 #ifdef __WINE__
2403 case ID_DRIVE_UNIX_FS: {
2404 TCHAR path[MAX_PATH];
2405 #ifdef UNICODE
2406 char cpath[MAX_PATH];
2407 #endif
2408 ChildWnd* child;
2409
2410 if (activate_fs_window(RS(b1,IDS_UNIXFS)))
2411 break;
2412
2413 #ifdef UNICODE
2414 getcwd(cpath, MAX_PATH);
2415 MultiByteToWideChar(CP_UNIXCP, 0, cpath, -1, path, MAX_PATH);
2416 #else
2417 getcwd(path, MAX_PATH);
2418 #endif
2419 child = alloc_child_window(path, NULL, hwnd);
2420
2421 if (!create_child_window(child))
2422 HeapFree(GetProcessHeap(), 0, child);
2423 break;}
2424 #endif
2425 #ifdef _SHELL_FOLDERS
2426 case ID_DRIVE_SHELL_NS: {
2427 TCHAR path[MAX_PATH];
2428 ChildWnd* child;
2429
2430 if (activate_fs_window(RS(b1,IDS_SHELL)))
2431 break;
2432
2433 GetCurrentDirectory(MAX_PATH, path);
2434 child = alloc_child_window(path, get_path_pidl(path,hwnd), hwnd);
2435
2436 if (!create_child_window(child))
2437 HeapFree(GetProcessHeap(), 0, child);
2438 break;}
2439 #endif
2440 #endif
2441
2442 /*TODO: There are even more menu items! */
2443
2444 case ID_ABOUT:
2445 ShellAbout(hwnd, RS(b1,IDS_WINEFILE), NULL,
2446 LoadImage( Globals.hInstance, MAKEINTRESOURCE(IDI_WINEFILE),
2447 IMAGE_ICON, 48, 48, LR_SHARED ));
2448 break;
2449
2450 default:
2451 /*TODO: if (wParam >= PM_FIRST_LANGUAGE && wParam <= PM_LAST_LANGUAGE)
2452 STRING_SelectLanguageByNumber(wParam - PM_FIRST_LANGUAGE);
2453 else */if ((cmd<IDW_FIRST_CHILD || cmd>=IDW_FIRST_CHILD+0x100) &&
2454 (cmd<SC_SIZE || cmd>SC_RESTORE))
2455 MessageBox(hwnd, RS(b2,IDS_NO_IMPL), RS(b1,IDS_WINEFILE), MB_OK);
2456
2457 return DefFrameProc(hwnd, Globals.hmdiclient, nmsg, wparam, lparam);
2458 }
2459 break;}
2460
2461 case WM_SIZE:
2462 resize_frame(hwnd, LOWORD(lparam), HIWORD(lparam));
2463 break; /* do not pass message to DefFrameProc */
2464
2465 case WM_DEVICECHANGE:
2466 SendMessage(hwnd, WM_COMMAND, MAKELONG(ID_REFRESH,0), 0);
2467 break;
2468
2469 #ifndef _NO_EXTENSIONS
2470 case WM_GETMINMAXINFO: {
2471 LPMINMAXINFO lpmmi = (LPMINMAXINFO)lparam;
2472
2473 lpmmi->ptMaxTrackSize.x <<= 1;/*2*GetSystemMetrics(SM_CXSCREEN) / SM_CXVIRTUALSCREEN */
2474 lpmmi->ptMaxTrackSize.y <<= 1;/*2*GetSystemMetrics(SM_CYSCREEN) / SM_CYVIRTUALSCREEN */
2475 break;}
2476
2477 case FRM_CALC_CLIENT:
2478 frame_get_clientspace(hwnd, (PRECT)lparam);
2479 return TRUE;
2480 #endif /* _NO_EXTENSIONS */
2481
2482 default:
2483 return DefFrameProc(hwnd, Globals.hmdiclient, nmsg, wparam, lparam);
2484 }
2485
2486 return 0;
2487 }
2488
2489
2490 static TCHAR g_pos_names[COLUMNS][20] = {
2491 {'\0'} /* symbol */
2492 };
2493
2494 static const int g_pos_align[] = {
2495 0,
2496 HDF_LEFT, /* Name */
2497 HDF_RIGHT, /* Size */
2498 HDF_LEFT, /* CDate */
2499 #ifndef _NO_EXTENSIONS
2500 HDF_LEFT, /* ADate */
2501 HDF_LEFT, /* MDate */
2502 HDF_LEFT, /* Index */
2503 HDF_CENTER, /* Links */
2504 #endif
2505 HDF_CENTER, /* Attributes */
2506 #ifndef _NO_EXTENSIONS
2507 HDF_LEFT /* Security */
2508 #endif
2509 };
2510
2511 static void resize_tree(ChildWnd* child, int cx, int cy)
2512 {
2513 HDWP hdwp = BeginDeferWindowPos(4);
2514 RECT rt;
2515
2516 rt.left = 0;
2517 rt.top = 0;
2518 rt.right = cx;
2519 rt.bottom = cy;
2520
2521 cx = child->split_pos + SPLIT_WIDTH/2;
2522
2523 #ifndef _NO_EXTENSIONS
2524 {
2525 WINDOWPOS wp;
2526 HD_LAYOUT hdl;
2527
2528 hdl.prc = &rt;
2529 hdl.pwpos = &wp;
2530
2531 SendMessage(child->left.hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hdl);
2532
2533 DeferWindowPos(hdwp, child->left.hwndHeader, wp.hwndInsertAfter,
2534 wp.x-1, wp.y, child->split_pos-SPLIT_WIDTH/2+1, wp.cy, wp.flags);
2535 DeferWindowPos(hdwp, child->right.hwndHeader, wp.hwndInsertAfter,
2536 rt.left+cx+1, wp.y, wp.cx-cx+2, wp.cy, wp.flags);
2537 }
2538 #endif /* _NO_EXTENSIONS */
2539
2540 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);
2541 DeferWindowPos(hdwp, child->right.hwnd, 0, rt.left+cx+1, rt.top, rt.right-cx, rt.bottom-rt.top, SWP_NOZORDER|SWP_NOACTIVATE);
2542
2543 EndDeferWindowPos(hdwp);
2544 }
2545
2546
2547 #ifndef _NO_EXTENSIONS
2548
2549 static HWND create_header(HWND parent, Pane* pane, UINT id)
2550 {
2551 HD_ITEM hdi;
2552 int idx;
2553
2554 HWND hwnd = CreateWindow(WC_HEADER, 0, WS_CHILD|WS_VISIBLE|HDS_HORZ|HDS_FULLDRAG/*TODO: |HDS_BUTTONS + sort orders*/,
2555 0, 0, 0, 0, parent, (HMENU)ULongToHandle(id), Globals.hInstance, 0);
2556 if (!hwnd)
2557 return 0;
2558
2559 SendMessage(hwnd, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), FALSE);
2560
2561 hdi.mask = HDI_TEXT|HDI_WIDTH|HDI_FORMAT;
2562
2563 for(idx=0; idx<COLUMNS; idx++) {
2564 hdi.pszText = g_pos_names[idx];
2565 hdi.fmt = HDF_STRING | g_pos_align[idx];
2566 hdi.cxy = pane->widths[idx];
2567 SendMessage(hwnd, HDM_INSERTITEM, idx, (LPARAM) &hdi);
2568 }
2569
2570 return hwnd;
2571 }
2572
2573 #endif /* _NO_EXTENSIONS */
2574
2575
2576 static void init_output(HWND hwnd)
2577 {
2578 static const WCHAR s1000[] = {'1','0','0','0','\0'};
2579 WCHAR b[16];
2580 HFONT old_font;
2581 HDC hdc = GetDC(hwnd);
2582
2583 if (GetNumberFormatW(LOCALE_USER_DEFAULT, 0, s1000, 0, b, 16) > 4)
2584 Globals.num_sep = b[1];
2585 else
2586 Globals.num_sep = '.';
2587
2588 old_font = SelectObject(hdc, Globals.hfont);
2589 GetTextExtentPoint32W(hdc, sSpace, 1, &Globals.spaceSize);
2590 SelectObject(hdc, old_font);
2591 ReleaseDC(hwnd, hdc);
2592 }
2593
2594 static void draw_item(Pane* pane, LPDRAWITEMSTRUCT dis, Entry* entry, int calcWidthCol);
2595
2596
2597 /* calculate preferred width for all visible columns */
2598
2599 static BOOL calc_widths(Pane* pane, BOOL anyway)
2600 {
2601 int col, x, cx, spc=3*Globals.spaceSize.cx;
2602 int entries = SendMessage(pane->hwnd, LB_GETCOUNT, 0, 0);
2603 int orgWidths[COLUMNS];
2604 int orgPositions[COLUMNS+1];
2605 HFONT hfontOld;
2606 HDC hdc;
2607 int cnt;
2608
2609 if (!anyway) {
2610 memcpy(orgWidths, pane->widths, sizeof(orgWidths));
2611 memcpy(orgPositions, pane->positions, sizeof(orgPositions));
2612 }
2613
2614 for(col=0; col<COLUMNS; col++)
2615 pane->widths[col] = 0;
2616
2617 hdc = GetDC(pane->hwnd);
2618 hfontOld = SelectObject(hdc, Globals.hfont);
2619
2620 for(cnt=0; cnt<entries; cnt++) {
2621 Entry* entry = (Entry*) SendMessage(pane->hwnd, LB_GETITEMDATA, cnt, 0);
2622
2623 DRAWITEMSTRUCT dis;
2624
2625 dis.CtlType = 0;
2626 dis.CtlID = 0;
2627 dis.itemID = 0;
2628 dis.itemAction = 0;
2629 dis.itemState = 0;
2630 dis.hwndItem = pane->hwnd;
2631 dis.hDC = hdc;
2632 dis.rcItem.left = 0;
2633 dis.rcItem.top = 0;
2634 dis.rcItem.right = 0;
2635 dis.rcItem.bottom = 0;
2636 /*dis.itemData = 0; */
2637
2638 draw_item(pane, &dis, entry, COLUMNS);
2639 }
2640
2641 SelectObject(hdc, hfontOld);
2642 ReleaseDC(pane->hwnd, hdc);
2643
2644 x = 0;
2645 for(col=0; col<COLUMNS; col++) {
2646 pane->positions[col] = x;
2647 cx = pane->widths[col];
2648
2649 if (cx) {
2650 cx += spc;
2651
2652 if (cx < IMAGE_WIDTH)
2653 cx = IMAGE_WIDTH;
2654
2655 pane->widths[col] = cx;
2656 }
2657
2658 x += cx;
2659 }
2660
2661 pane->positions[COLUMNS] = x;
2662
2663 SendMessage(pane->hwnd, LB_SETHORIZONTALEXTENT, x, 0);
2664
2665 /* no change? */
2666 if (!anyway && !memcmp(orgWidths, pane->widths, sizeof(orgWidths)))
2667 return FALSE;
2668
2669 /* don't move, if only collapsing an entry */
2670 if (!anyway && pane->widths[0]<orgWidths[0] &&
2671 !memcmp(orgWidths+1, pane->widths+1, sizeof(orgWidths)-sizeof(int))) {
2672 pane->widths[0] = orgWidths[0];
2673 memcpy(pane->positions, orgPositions, sizeof(orgPositions));
2674
2675 return FALSE;
2676 }
2677
2678 InvalidateRect(pane->hwnd, 0, TRUE);
2679
2680 return TRUE;
2681 }
2682
2683
2684 /* calculate one preferred column width */
2685
2686 static void calc_single_width(Pane* pane, int col)
2687 {
2688 HFONT hfontOld;
2689 int x, cx;
2690 int entries = SendMessage(pane->hwnd, LB_GETCOUNT, 0, 0);
2691 int cnt;
2692 HDC hdc;
2693
2694 pane->widths[col] = 0;
2695
2696 hdc = GetDC(pane->hwnd);
2697 hfontOld = SelectObject(hdc, Globals.hfont);
2698
2699 for(cnt=0; cnt<entries; cnt++) {
2700 Entry* entry = (Entry*) SendMessage(pane->hwnd, LB_GETITEMDATA, cnt, 0);
2701 DRAWITEMSTRUCT dis;
2702
2703 dis.CtlType = 0;
2704 dis.CtlID = 0;
2705 dis.itemID = 0;
2706 dis.itemAction = 0;
2707 dis.itemState = 0;
2708 dis.hwndItem = pane->hwnd;
2709 dis.hDC = hdc;
2710 dis.rcItem.left = 0;
2711 dis.rcItem.top = 0;
2712 dis.rcItem.right = 0;
2713 dis.rcItem.bottom = 0;
2714 /*dis.itemData = 0; */
2715
2716 draw_item(pane, &dis, entry, col);
2717 }
2718
2719 SelectObject(hdc, hfontOld);
2720 ReleaseDC(pane->hwnd, hdc);
2721
2722 cx = pane->widths[col];
2723
2724 if (cx) {
2725 cx += 3*Globals.spaceSize.cx;
2726
2727 if (cx < IMAGE_WIDTH)
2728 cx = IMAGE_WIDTH;
2729 }
2730
2731 pane->widths[col] = cx;
2732
2733 x = pane->positions[col] + cx;
2734
2735 for(; col<COLUMNS; ) {
2736 pane->positions[++col] = x;
2737 x += pane->widths[col];
2738 }
2739
2740 SendMessage(pane->hwnd, LB_SETHORIZONTALEXTENT, x, 0);
2741 }
2742
2743
2744 static BOOL pattern_match(LPCTSTR str, LPCTSTR pattern)
2745 {
2746 for( ; *str&&*pattern; str++,pattern++) {
2747 if (*pattern == '*') {
2748 do pattern++;
2749 while(*pattern == '*');
2750
2751 if (!*pattern)
2752 return TRUE;
2753
2754 for(; *str; str++)
2755 if (*str==*pattern && pattern_match(str, pattern))
2756 return TRUE;
2757
2758 return FALSE;
2759 }
2760 else if (*str!=*pattern && *pattern!='?')
2761 return FALSE;
2762 }
2763
2764 if (*str || *pattern)
2765 if (*pattern!='*' || pattern[1]!='\0')
2766 return FALSE;
2767
2768 return TRUE;
2769 }
2770
2771 static BOOL pattern_imatch(LPCTSTR str, LPCTSTR pattern)
2772 {
2773 TCHAR b1[BUFFER_LEN], b2[BUFFER_LEN];
2774
2775 lstrcpy(b1, str);
2776 lstrcpy(b2, pattern);
2777 CharUpper(b1);
2778 CharUpper(b2);
2779
2780 return pattern_match(b1, b2);
2781 }
2782
2783
2784 enum FILE_TYPE {
2785 FT_OTHER = 0,
2786 FT_EXECUTABLE = 1,
2787 FT_DOCUMENT = 2
2788 };
2789
2790 static enum FILE_TYPE get_file_type(LPCTSTR filename);
2791
2792
2793 /* insert listbox entries after index idx */
2794
2795 static int insert_entries(Pane* pane, Entry* dir, LPCTSTR pattern, int filter_flags, int idx)
2796 {
2797 Entry* entry = dir;
2798
2799 if (!entry)
2800 return idx;
2801
2802 ShowWindow(pane->hwnd, SW_HIDE);
2803
2804 for(; entry; entry=entry->next) {
2805 #ifndef _LEFT_FILES
2806 if (pane->treePane && !(entry->data.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY))
2807 continue;
2808 #endif
2809
2810 if (entry->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
2811 /* don't display entries "." and ".." in the left pane */
2812 if (pane->treePane && entry->data.cFileName[0] == '.')
2813 if (
2814 #ifndef _NO_EXTENSIONS
2815 entry->data.cFileName[1] == '\0' ||
2816 #endif
2817 (entry->data.cFileName[1] == '.' && entry->data.cFileName[2] == '\0'))
2818 continue;
2819
2820 /* filter directories in right pane */
2821 if (!pane->treePane && !(filter_flags&TF_DIRECTORIES))
2822 continue;
2823 }
2824
2825 /* filter using the file name pattern */
2826 if (pattern)
2827 if (!pattern_imatch(entry->data.cFileName, pattern))
2828 continue;
2829
2830 /* filter system and hidden files */
2831 if (!(filter_flags&TF_HIDDEN) && (entry->data.dwFileAttributes&(FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM)))
2832 continue;
2833
2834 /* filter looking at the file type */
2835 if ((filter_flags&(TF_PROGRAMS|TF_DOCUMENTS|TF_OTHERS)) != (TF_PROGRAMS|TF_DOCUMENTS|TF_OTHERS))
2836 switch(get_file_type(entry->data.cFileName)) {
2837 case FT_EXECUTABLE:
2838 if (!(filter_flags & TF_PROGRAMS))
2839 continue;
2840 break;
2841
2842 case FT_DOCUMENT:
2843 if (!(filter_flags & TF_DOCUMENTS))
2844 continue;
2845 break;
2846
2847 default: /* TF_OTHERS */
2848 if (!(filter_flags & TF_OTHERS))
2849 continue;
2850 }
2851
2852 if (idx != -1)
2853 idx++;
2854
2855 SendMessage(pane->hwnd, LB_INSERTSTRING, idx, (LPARAM) entry);
2856
2857 if (pane->treePane && entry->expanded)
2858 idx = insert_entries(pane, entry->down, pattern, filter_flags, idx);
2859 }
2860
2861 ShowWindow(pane->hwnd, SW_SHOW);
2862
2863 return idx;
2864 }
2865
2866
2867 static void format_bytes(LPTSTR buffer, LONGLONG bytes)
2868 {
2869 static const TCHAR sFmtGB[] = {'%', '.', '1', 'f', ' ', 'G', 'B', '\0'};
2870 static const TCHAR sFmtMB[] = {'%', '.', '1', 'f', ' ', 'M', 'B', '\0'};
2871 static const TCHAR sFmtkB[] = {'%', '.', '1', 'f', ' ', 'k', 'B', '\0'};
2872 static const TCHAR sFmtB[] = {'%', 'u', 0};
2873
2874 float fBytes = (float)bytes;
2875
2876 if (bytes >= 1073741824) /* 1 GB */
2877 sprintfW(buffer, sFmtGB, fBytes/1073741824.f+.5f);
2878 else if (bytes >= 1048576) /* 1 MB */
2879 sprintfW(buffer, sFmtMB, fBytes/1048576.f+.5f);
2880 else if (bytes >= 1024) /* 1 kB */
2881 sprintfW(buffer, sFmtkB, fBytes/1024.f+.5f);
2882 else
2883 sprintfW(buffer, sFmtB, (DWORD)bytes);
2884 }
2885
2886 static void set_space_status(void)
2887 {
2888 ULARGE_INTEGER ulFreeBytesToCaller, ulTotalBytes, ulFreeBytes;
2889 TCHAR fmt[64], b1[64], b2[64], buffer[BUFFER_LEN];
2890
2891 if (GetDiskFreeSpaceEx(NULL, &ulFreeBytesToCaller, &ulTotalBytes, &ulFreeBytes)) {
2892 format_bytes(b1, ulFreeBytesToCaller.QuadPart);
2893 format_bytes(b2, ulTotalBytes.QuadPart);
2894 wsprintf(buffer, RS(fmt,IDS_FREE_SPACE_FMT), b1, b2);
2895 } else
2896 lstrcpy(buffer, sQMarks);
2897
2898 SendMessage(Globals.hstatusbar, SB_SETTEXT, 0, (LPARAM)buffer);
2899 }
2900
2901
2902 static WNDPROC g_orgTreeWndProc;
2903
2904 static void create_tree_window(HWND parent, Pane* pane, UINT id, UINT id_header, LPCTSTR pattern, int filter_flags)
2905 {
2906 static const TCHAR sListBox[] = {'L','i','s','t','B','o','x','\0'};
2907
2908 static int s_init = 0;
2909 Entry* entry = pane->root;
2910
2911 pane->hwnd = CreateWindow(sListBox, sEmpty, WS_CHILD|WS_VISIBLE|WS_HSCROLL|WS_VSCROLL|
2912 LBS_DISABLENOSCROLL|LBS_NOINTEGRALHEIGHT|LBS_OWNERDRAWFIXED|LBS_NOTIFY,
2913 0, 0, 0, 0, parent, (HMENU)ULongToHandle(id), Globals.hInstance, 0);
2914
2915 SetWindowLongPtr(pane->hwnd, GWLP_USERDATA, (LPARAM)pane);
2916 g_orgTreeWndProc = (WNDPROC) SetWindowLongPtr(pane->hwnd, GWLP_WNDPROC, (LPARAM)TreeWndProc);
2917
2918 SendMessage(pane->hwnd, WM_SETFONT, (WPARAM)Globals.hfont, FALSE);
2919
2920 /* insert entries into listbox */
2921 if (entry)
2922 insert_entries(pane, entry, pattern, filter_flags, -1);
2923
2924 /* calculate column widths */
2925 if (!s_init) {
2926 s_init = 1;
2927 init_output(pane->hwnd);
2928 }
2929
2930 calc_widths(pane, TRUE);
2931
2932 #ifndef _NO_EXTENSIONS
2933 pane->hwndHeader = create_header(parent, pane, id_header);
2934 #endif
2935 }
2936
2937
2938 static void InitChildWindow(ChildWnd* child)
2939 {
2940 create_tree_window(child->hwnd, &child->left, IDW_TREE_LEFT, IDW_HEADER_LEFT, NULL, TF_ALL);
2941 create_tree_window(child->hwnd, &child->right, IDW_TREE_RIGHT, IDW_HEADER_RIGHT, child->filter_pattern, child->filter_flags);
2942 }
2943
2944
2945 static void format_date(const FILETIME* ft, TCHAR* buffer, int visible_cols)
2946 {
2947 SYSTEMTIME systime;
2948 FILETIME lft;
2949 int len = 0;
2950
2951 *buffer = '\0';
2952
2953 if (!ft->dwLowDateTime && !ft->dwHighDateTime)
2954 return;
2955
2956 if (!FileTimeToLocalFileTime(ft, &lft))
2957 {err: lstrcpy(buffer,sQMarks); return;}
2958
2959 if (!FileTimeToSystemTime(&lft, &systime))
2960 goto err;
2961
2962 if (visible_cols & COL_DATE) {
2963 len = GetDateFormat(LOCALE_USER_DEFAULT, 0, &systime, 0, buffer, BUFFER_LEN);
2964 if (!len)
2965 goto err;
2966 }
2967
2968 if (visible_cols & COL_TIME) {
2969 if (len)
2970 buffer[len-1] = ' ';
2971
2972 buffer[len++] = ' ';
2973
2974 if (!GetTimeFormat(LOCALE_USER_DEFAULT, 0, &systime, 0, buffer+len, BUFFER_LEN-len))
2975 buffer[len] = '\0';
2976 }
2977 }
2978
2979
2980 static void calc_width(Pane* pane, LPDRAWITEMSTRUCT dis, int col, LPCTSTR str)
2981 {
2982 RECT rt = {0, 0, 0, 0};
2983
2984 DrawText(dis->hDC, str, -1, &rt, DT_CALCRECT|DT_SINGLELINE|DT_NOPREFIX);
2985
2986 if (rt.right > pane->widths[col])
2987 pane->widths[col] = rt.right;
2988 }
2989
2990 static void calc_tabbed_width(Pane* pane, LPDRAWITEMSTRUCT dis, int col, LPCTSTR str)
2991 {
2992 RECT rt = {0, 0, 0, 0};
2993
2994 /* DRAWTEXTPARAMS dtp = {sizeof(DRAWTEXTPARAMS), 2};
2995 DrawTextEx(dis->hDC, (LPTSTR)str, -1, &rt, DT_CALCRECT|DT_SINGLELINE|DT_NOPREFIX|DT_EXPANDTABS|DT_TABSTOP, &dtp);*/
2996
2997 DrawText(dis->hDC, str, -1, &rt, DT_CALCRECT|DT_SINGLELINE|DT_EXPANDTABS|DT_TABSTOP|(2<<8));
2998 /*FIXME rt (0,0) ??? */
2999
3000 if (rt.right > pane->widths[col])
3001 pane->widths[col] = rt.right;
3002 }
3003
3004
3005 static void output_text(Pane* pane, LPDRAWITEMSTRUCT dis, int col, LPCTSTR str, DWORD flags)
3006 {
3007 int x = dis->rcItem.left;
3008 RECT rt