xaudio2: Explicitly version the 2.7 coclasses.
[wine.git] / programs / winefile / winefile.c
blob9d3f0e1ea1131bcdbc33b82f9cdb4eed090e746f
1 /*
2 * Winefile
4 * Copyright 2000, 2003, 2004, 2005 Martin Fuchs
5 * Copyright 2006 Jason Green
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.
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.
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
22 #ifdef __WINE__
23 #include "config.h"
24 #include "wine/port.h"
26 /* for unix filesystem function calls */
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <dirent.h>
30 #endif
32 #define COBJMACROS
34 #include "winefile.h"
35 #include "resource.h"
36 #include "wine/unicode.h"
38 #ifndef _MAX_PATH
39 #define _MAX_DRIVE 3
40 #define _MAX_FNAME 256
41 #define _MAX_DIR _MAX_FNAME
42 #define _MAX_EXT _MAX_FNAME
43 #define _MAX_PATH 260
44 #endif
46 #ifdef NONAMELESSUNION
47 #define UNION_MEMBER(x) DUMMYUNIONNAME.x
48 #else
49 #define UNION_MEMBER(x) x
50 #endif
52 #define DEFAULT_SPLIT_POS 300
54 static const WCHAR registry_key[] = { 'S','o','f','t','w','a','r','e','\\',
55 'W','i','n','e','\\',
56 'W','i','n','e','F','i','l','e','\0'};
57 static const WCHAR reg_start_x[] = { 's','t','a','r','t','X','\0'};
58 static const WCHAR reg_start_y[] = { 's','t','a','r','t','Y','\0'};
59 static const WCHAR reg_width[] = { 'w','i','d','t','h','\0'};
60 static const WCHAR reg_height[] = { 'h','e','i','g','h','t','\0'};
61 static const WCHAR reg_logfont[] = { 'l','o','g','f','o','n','t','\0'};
63 enum ENTRY_TYPE {
64 ET_WINDOWS,
65 ET_UNIX,
66 ET_SHELL
69 typedef struct _Entry {
70 struct _Entry* next;
71 struct _Entry* down;
72 struct _Entry* up;
74 BOOL expanded;
75 BOOL scanned;
76 int level;
78 WIN32_FIND_DATAW data;
80 BY_HANDLE_FILE_INFORMATION bhfi;
81 BOOL bhfi_valid;
82 enum ENTRY_TYPE etype;
83 LPITEMIDLIST pidl;
84 IShellFolder* folder;
85 HICON hicon;
86 } Entry;
88 typedef struct {
89 Entry entry;
90 WCHAR path[MAX_PATH];
91 WCHAR volname[_MAX_FNAME];
92 WCHAR fs[_MAX_DIR];
93 DWORD drive_type;
94 DWORD fs_flags;
95 } Root;
97 enum COLUMN_FLAGS {
98 COL_SIZE = 0x01,
99 COL_DATE = 0x02,
100 COL_TIME = 0x04,
101 COL_ATTRIBUTES = 0x08,
102 COL_DOSNAMES = 0x10,
103 COL_INDEX = 0x20,
104 COL_LINKS = 0x40,
105 COL_ALL = COL_SIZE|COL_DATE|COL_TIME|COL_ATTRIBUTES|COL_DOSNAMES|COL_INDEX|COL_LINKS
108 typedef enum {
109 SORT_NAME,
110 SORT_EXT,
111 SORT_SIZE,
112 SORT_DATE
113 } SORT_ORDER;
115 typedef struct {
116 HWND hwnd;
117 HWND hwndHeader;
119 #define COLUMNS 10
120 int widths[COLUMNS];
121 int positions[COLUMNS+1];
123 BOOL treePane;
124 int visible_cols;
125 Entry* root;
126 Entry* cur;
127 } Pane;
129 typedef struct {
130 HWND hwnd;
131 Pane left;
132 Pane right;
133 int focus_pane; /* 0: left 1: right */
134 WINDOWPLACEMENT pos;
135 int split_pos;
136 BOOL header_wdths_ok;
138 WCHAR path[MAX_PATH];
139 WCHAR filter_pattern[MAX_PATH];
140 int filter_flags;
141 Root root;
143 SORT_ORDER sortOrder;
144 } ChildWnd;
148 static void read_directory(Entry* dir, LPCWSTR path, SORT_ORDER sortOrder, HWND hwnd);
149 static void set_curdir(ChildWnd* child, Entry* entry, int idx, HWND hwnd);
150 static void refresh_child(ChildWnd* child);
151 static void refresh_drives(void);
152 static void get_path(Entry* dir, PWSTR path);
153 static void format_date(const FILETIME* ft, WCHAR* buffer, int visible_cols);
155 static LRESULT CALLBACK FrameWndProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam);
156 static LRESULT CALLBACK ChildWndProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam);
157 static LRESULT CALLBACK TreeWndProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam);
160 /* globals */
161 WINEFILE_GLOBALS Globals;
163 static int last_split;
165 /* some common string constants */
166 static const WCHAR sEmpty[] = {'\0'};
167 static const WCHAR sSpace[] = {' ', '\0'};
168 static const WCHAR sNumFmt[] = {'%','d','\0'};
169 static const WCHAR sQMarks[] = {'?','?','?','\0'};
171 /* window class names */
172 static const WCHAR sWINEFILEFRAME[] = {'W','F','S','_','F','r','a','m','e','\0'};
173 static const WCHAR sWINEFILETREE[] = {'W','F','S','_','T','r','e','e','\0'};
175 static void format_longlong(LPWSTR ret, ULONGLONG val)
177 WCHAR buffer[65], *p = &buffer[64];
179 *p = 0;
180 do {
181 *(--p) = '0' + val % 10;
182 val /= 10;
183 } while (val);
184 lstrcpyW( ret, p );
188 /* load resource string */
189 static LPWSTR load_string(LPWSTR buffer, DWORD size, UINT id)
191 LoadStringW(Globals.hInstance, id, buffer, size);
192 return buffer;
195 #define RS(b, i) load_string(b, sizeof(b)/sizeof(b[0]), i)
198 /* display error message for the specified WIN32 error code */
199 static void display_error(HWND hwnd, DWORD error)
201 WCHAR b1[BUFFER_LEN], b2[BUFFER_LEN];
202 PWSTR msg;
204 if (FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
205 0, error, MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), (PWSTR)&msg, 0, NULL))
206 MessageBoxW(hwnd, msg, RS(b2,IDS_WINEFILE), MB_OK);
207 else
208 MessageBoxW(hwnd, RS(b1,IDS_ERROR), RS(b2,IDS_WINEFILE), MB_OK);
210 LocalFree(msg);
214 /* display network error message using WNetGetLastErrorW() */
215 static void display_network_error(HWND hwnd)
217 WCHAR msg[BUFFER_LEN], provider[BUFFER_LEN], b2[BUFFER_LEN];
218 DWORD error;
220 if (WNetGetLastErrorW(&error, msg, BUFFER_LEN, provider, BUFFER_LEN) == NO_ERROR)
221 MessageBoxW(hwnd, msg, RS(b2,IDS_WINEFILE), MB_OK);
224 static inline BOOL get_check(HWND hwnd, INT id)
226 return BST_CHECKED&SendMessageW(GetDlgItem(hwnd, id), BM_GETSTATE, 0, 0);
229 static inline INT set_check(HWND hwnd, INT id, BOOL on)
231 return SendMessageW(GetDlgItem(hwnd, id), BM_SETCHECK, on?BST_CHECKED:BST_UNCHECKED, 0);
234 static inline void choose_font(HWND hwnd)
236 WCHAR dlg_name[BUFFER_LEN], dlg_info[BUFFER_LEN];
237 CHOOSEFONTW chFont;
238 LOGFONTW lFont;
240 HDC hdc = GetDC(hwnd);
242 GetObjectW(Globals.hfont, sizeof(LOGFONTW), &lFont);
244 chFont.lStructSize = sizeof(CHOOSEFONTW);
245 chFont.hwndOwner = hwnd;
246 chFont.hDC = NULL;
247 chFont.lpLogFont = &lFont;
248 chFont.Flags = CF_SCREENFONTS | CF_FORCEFONTEXIST | CF_LIMITSIZE | CF_NOSCRIPTSEL | CF_INITTOLOGFONTSTRUCT | CF_NOVERTFONTS;
249 chFont.rgbColors = RGB(0,0,0);
250 chFont.lCustData = 0;
251 chFont.lpfnHook = NULL;
252 chFont.lpTemplateName = NULL;
253 chFont.hInstance = Globals.hInstance;
254 chFont.lpszStyle = NULL;
255 chFont.nFontType = SIMULATED_FONTTYPE;
256 chFont.nSizeMin = 0;
257 chFont.nSizeMax = 24;
259 if (ChooseFontW(&chFont)) {
260 HWND childWnd;
261 HFONT hFontOld;
263 DeleteObject(Globals.hfont);
264 Globals.hfont = CreateFontIndirectW(&lFont);
265 hFontOld = SelectObject(hdc, Globals.hfont);
266 GetTextExtentPoint32W(hdc, sSpace, 1, &Globals.spaceSize);
268 /* change font in all open child windows */
269 for(childWnd=GetWindow(Globals.hmdiclient,GW_CHILD); childWnd; childWnd=GetNextWindow(childWnd,GW_HWNDNEXT)) {
270 ChildWnd* child = (ChildWnd*) GetWindowLongPtrW(childWnd, GWLP_USERDATA);
271 SendMessageW(child->left.hwnd, WM_SETFONT, (WPARAM)Globals.hfont, TRUE);
272 SendMessageW(child->right.hwnd, WM_SETFONT, (WPARAM)Globals.hfont, TRUE);
273 SendMessageW(child->left.hwnd, LB_SETITEMHEIGHT, 1, max(Globals.spaceSize.cy,IMAGE_HEIGHT+3));
274 SendMessageW(child->right.hwnd, LB_SETITEMHEIGHT, 1, max(Globals.spaceSize.cy,IMAGE_HEIGHT+3));
275 InvalidateRect(child->left.hwnd, NULL, TRUE);
276 InvalidateRect(child->right.hwnd, NULL, TRUE);
279 SelectObject(hdc, hFontOld);
281 else if (CommDlgExtendedError()) {
282 LoadStringW(Globals.hInstance, IDS_FONT_SEL_DLG_NAME, dlg_name, BUFFER_LEN);
283 LoadStringW(Globals.hInstance, IDS_FONT_SEL_ERROR, dlg_info, BUFFER_LEN);
284 MessageBoxW(hwnd, dlg_info, dlg_name, MB_OK);
287 ReleaseDC(hwnd, hdc);
291 /* allocate and initialise a directory entry */
292 static Entry* alloc_entry(void)
294 Entry* entry = HeapAlloc(GetProcessHeap(), 0, sizeof(Entry));
296 entry->pidl = NULL;
297 entry->folder = NULL;
298 entry->hicon = 0;
300 return entry;
303 /* free a directory entry */
304 static void free_entry(Entry* entry)
306 if (entry->hicon && entry->hicon!=(HICON)-1)
307 DestroyIcon(entry->hicon);
309 if (entry->folder && entry->folder!=Globals.iDesktop)
310 IShellFolder_Release(entry->folder);
312 if (entry->pidl)
313 IMalloc_Free(Globals.iMalloc, entry->pidl);
315 HeapFree(GetProcessHeap(), 0, entry);
318 /* recursively free all child entries */
319 static void free_entries(Entry* dir)
321 Entry *entry, *next=dir->down;
323 if (next) {
324 dir->down = 0;
326 do {
327 entry = next;
328 next = entry->next;
330 free_entries(entry);
331 free_entry(entry);
332 } while(next);
337 static void read_directory_win(Entry* dir, LPCWSTR path)
339 Entry* first_entry = NULL;
340 Entry* last = NULL;
341 Entry* entry;
343 int level = dir->level + 1;
344 WIN32_FIND_DATAW w32fd;
345 HANDLE hFind;
346 HANDLE hFile;
348 WCHAR buffer[MAX_PATH], *p;
349 for(p=buffer; *path; )
350 *p++ = *path++;
352 *p++ = '\\';
353 p[0] = '*';
354 p[1] = '\0';
356 hFind = FindFirstFileW(buffer, &w32fd);
358 if (hFind != INVALID_HANDLE_VALUE) {
359 do {
360 entry = alloc_entry();
362 if (!first_entry)
363 first_entry = entry;
365 if (last)
366 last->next = entry;
368 memcpy(&entry->data, &w32fd, sizeof(WIN32_FIND_DATAW));
369 entry->down = NULL;
370 entry->up = dir;
371 entry->expanded = FALSE;
372 entry->scanned = FALSE;
373 entry->level = level;
374 entry->etype = ET_WINDOWS;
375 entry->bhfi_valid = FALSE;
377 lstrcpyW(p, entry->data.cFileName);
379 hFile = CreateFileW(buffer, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
380 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0);
382 if (hFile != INVALID_HANDLE_VALUE) {
383 if (GetFileInformationByHandle(hFile, &entry->bhfi))
384 entry->bhfi_valid = TRUE;
386 CloseHandle(hFile);
389 last = entry;
390 } while(FindNextFileW(hFind, &w32fd));
392 if (last)
393 last->next = NULL;
395 FindClose(hFind);
398 dir->down = first_entry;
399 dir->scanned = TRUE;
403 static Entry* find_entry_win(Entry* dir, LPCWSTR name)
405 Entry* entry;
407 for(entry=dir->down; entry; entry=entry->next) {
408 LPCWSTR p = name;
409 LPCWSTR q = entry->data.cFileName;
411 do {
412 if (!*p || *p == '\\' || *p == '/')
413 return entry;
414 } while(tolower(*p++) == tolower(*q++));
416 p = name;
417 q = entry->data.cAlternateFileName;
419 do {
420 if (!*p || *p == '\\' || *p == '/')
421 return entry;
422 } while(tolower(*p++) == tolower(*q++));
425 return 0;
429 static Entry* read_tree_win(Root* root, LPCWSTR path, SORT_ORDER sortOrder, HWND hwnd)
431 WCHAR buffer[MAX_PATH];
432 Entry* entry = &root->entry;
433 LPCWSTR s = path;
434 PWSTR d = buffer;
436 HCURSOR old_cursor = SetCursor(LoadCursorW(0, (LPCWSTR)IDC_WAIT));
438 entry->etype = ET_WINDOWS;
439 while(entry) {
440 while(*s && *s != '\\' && *s != '/')
441 *d++ = *s++;
443 while(*s == '\\' || *s == '/')
444 s++;
446 *d++ = '\\';
447 *d = '\0';
449 read_directory(entry, buffer, sortOrder, hwnd);
451 if (entry->down)
452 entry->expanded = TRUE;
454 if (!*s)
455 break;
457 entry = find_entry_win(entry, s);
460 SetCursor(old_cursor);
462 return entry;
466 #ifdef __WINE__
468 static BOOL time_to_filetime(time_t t, FILETIME* ftime)
470 struct tm* tm = gmtime(&t);
471 SYSTEMTIME stime;
473 if (!tm)
474 return FALSE;
476 stime.wYear = tm->tm_year+1900;
477 stime.wMonth = tm->tm_mon+1;
478 /* stime.wDayOfWeek */
479 stime.wDay = tm->tm_mday;
480 stime.wHour = tm->tm_hour;
481 stime.wMinute = tm->tm_min;
482 stime.wSecond = tm->tm_sec;
483 stime.wMilliseconds = 0;
485 return SystemTimeToFileTime(&stime, ftime);
488 static void read_directory_unix(Entry* dir, LPCWSTR path)
490 Entry* first_entry = NULL;
491 Entry* last = NULL;
492 Entry* entry;
493 DIR* pdir;
495 int level = dir->level + 1;
496 char cpath[MAX_PATH];
498 WideCharToMultiByte(CP_UNIXCP, 0, path, -1, cpath, MAX_PATH, NULL, NULL);
499 pdir = opendir(cpath);
501 if (pdir) {
502 struct stat st;
503 struct dirent* ent;
504 char buffer[MAX_PATH], *p;
505 const char* s;
507 for(p=buffer,s=cpath; *s; )
508 *p++ = *s++;
510 if (p==buffer || p[-1]!='/')
511 *p++ = '/';
513 while((ent=readdir(pdir))) {
514 entry = alloc_entry();
516 if (!first_entry)
517 first_entry = entry;
519 if (last)
520 last->next = entry;
522 entry->etype = ET_UNIX;
524 strcpy(p, ent->d_name);
525 MultiByteToWideChar(CP_UNIXCP, 0, p, -1, entry->data.cFileName, MAX_PATH);
527 if (!stat(buffer, &st)) {
528 entry->data.dwFileAttributes = p[0]=='.'? FILE_ATTRIBUTE_HIDDEN: 0;
530 if (S_ISDIR(st.st_mode))
531 entry->data.dwFileAttributes |= FILE_ATTRIBUTE_DIRECTORY;
533 entry->data.nFileSizeLow = st.st_size & 0xFFFFFFFF;
534 entry->data.nFileSizeHigh = st.st_size >> 32;
536 memset(&entry->data.ftCreationTime, 0, sizeof(FILETIME));
537 time_to_filetime(st.st_atime, &entry->data.ftLastAccessTime);
538 time_to_filetime(st.st_mtime, &entry->data.ftLastWriteTime);
540 entry->bhfi.nFileIndexLow = ent->d_ino;
541 entry->bhfi.nFileIndexHigh = 0;
543 entry->bhfi.nNumberOfLinks = st.st_nlink;
545 entry->bhfi_valid = TRUE;
546 } else {
547 entry->data.nFileSizeLow = 0;
548 entry->data.nFileSizeHigh = 0;
549 entry->bhfi_valid = FALSE;
552 entry->down = NULL;
553 entry->up = dir;
554 entry->expanded = FALSE;
555 entry->scanned = FALSE;
556 entry->level = level;
558 last = entry;
561 if (last)
562 last->next = NULL;
564 closedir(pdir);
567 dir->down = first_entry;
568 dir->scanned = TRUE;
571 static Entry* find_entry_unix(Entry* dir, LPCWSTR name)
573 Entry* entry;
575 for(entry=dir->down; entry; entry=entry->next) {
576 LPCWSTR p = name;
577 LPCWSTR q = entry->data.cFileName;
579 do {
580 if (!*p || *p == '/')
581 return entry;
582 } while(*p++ == *q++);
585 return 0;
588 static Entry* read_tree_unix(Root* root, LPCWSTR path, SORT_ORDER sortOrder, HWND hwnd)
590 WCHAR buffer[MAX_PATH];
591 Entry* entry = &root->entry;
592 LPCWSTR s = path;
593 PWSTR d = buffer;
595 HCURSOR old_cursor = SetCursor(LoadCursorW(0, (LPCWSTR)IDC_WAIT));
597 entry->etype = ET_UNIX;
599 while(entry) {
600 while(*s && *s != '/')
601 *d++ = *s++;
603 while(*s == '/')
604 s++;
606 *d++ = '/';
607 *d = '\0';
609 read_directory(entry, buffer, sortOrder, hwnd);
611 if (entry->down)
612 entry->expanded = TRUE;
614 if (!*s)
615 break;
617 entry = find_entry_unix(entry, s);
620 SetCursor(old_cursor);
622 return entry;
625 #endif /* __WINE__ */
627 static void free_strret(STRRET* str)
629 if (str->uType == STRRET_WSTR)
630 IMalloc_Free(Globals.iMalloc, str->UNION_MEMBER(pOleStr));
633 static LPWSTR wcscpyn(LPWSTR dest, LPCWSTR source, size_t count)
635 LPCWSTR s;
636 LPWSTR d = dest;
638 for(s=source; count&&(*d++=*s++); )
639 count--;
641 return dest;
644 static void get_strretW(STRRET* str, const SHITEMID* shiid, LPWSTR buffer, int len)
646 switch(str->uType) {
647 case STRRET_WSTR:
648 wcscpyn(buffer, str->UNION_MEMBER(pOleStr), len);
649 break;
651 case STRRET_OFFSET:
652 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)shiid+str->UNION_MEMBER(uOffset), -1, buffer, len);
653 break;
655 case STRRET_CSTR:
656 MultiByteToWideChar(CP_ACP, 0, str->UNION_MEMBER(cStr), -1, buffer, len);
661 static HRESULT name_from_pidl(IShellFolder* folder, LPITEMIDLIST pidl, LPWSTR buffer, int len, SHGDNF flags)
663 STRRET str;
665 HRESULT hr = IShellFolder_GetDisplayNameOf(folder, pidl, flags, &str);
667 if (SUCCEEDED(hr)) {
668 get_strretW(&str, &pidl->mkid, buffer, len);
669 free_strret(&str);
670 } else
671 buffer[0] = '\0';
673 return hr;
677 static HRESULT path_from_pidlW(IShellFolder* folder, LPITEMIDLIST pidl, LPWSTR buffer, int len)
679 STRRET str;
681 /* SHGDN_FORPARSING: get full path of id list */
682 HRESULT hr = IShellFolder_GetDisplayNameOf(folder, pidl, SHGDN_FORPARSING, &str);
684 if (SUCCEEDED(hr)) {
685 get_strretW(&str, &pidl->mkid, buffer, len);
686 free_strret(&str);
687 } else
688 buffer[0] = '\0';
690 return hr;
694 /* create an item id list from a file system path */
696 static LPITEMIDLIST get_path_pidl(LPWSTR path, HWND hwnd)
698 LPITEMIDLIST pidl;
699 HRESULT hr;
700 ULONG len;
701 LPWSTR buffer = path;
703 hr = IShellFolder_ParseDisplayName(Globals.iDesktop, hwnd, NULL, buffer, &len, &pidl, NULL);
704 if (FAILED(hr))
705 return NULL;
707 return pidl;
711 /* convert an item id list from relative to absolute (=relative to the desktop) format */
713 static LPITEMIDLIST get_to_absolute_pidl(Entry* entry, HWND hwnd)
715 if (entry->up && entry->up->etype==ET_SHELL) {
716 LPITEMIDLIST idl = NULL;
718 while (entry->up) {
719 idl = ILCombine(ILClone(entry->pidl), idl);
720 entry = entry->up;
723 return idl;
724 } else if (entry->etype == ET_WINDOWS) {
725 WCHAR path[MAX_PATH];
727 get_path(entry, path);
729 return get_path_pidl(path, hwnd);
730 } else if (entry->pidl)
731 return ILClone(entry->pidl);
733 return NULL;
737 static HICON extract_icon(IShellFolder* folder, LPCITEMIDLIST pidl)
739 IExtractIconW* pExtract;
741 if (SUCCEEDED(IShellFolder_GetUIObjectOf(folder, 0, 1, (LPCITEMIDLIST*)&pidl, &IID_IExtractIconW, 0, (LPVOID*)&pExtract))) {
742 WCHAR path[_MAX_PATH];
743 unsigned flags;
744 HICON hicon;
745 int idx;
747 if (SUCCEEDED(IExtractIconW_GetIconLocation(pExtract, GIL_FORSHELL, path, _MAX_PATH, &idx, &flags))) {
748 if (!(flags & GIL_NOTFILENAME)) {
749 if (idx == -1)
750 idx = 0; /* special case for some control panel applications */
752 if ((int)ExtractIconExW(path, idx, 0, &hicon, 1) > 0)
753 flags &= ~GIL_DONTCACHE;
754 } else {
755 HICON hIconLarge = 0;
757 HRESULT hr = IExtractIconW_Extract(pExtract, path, idx, &hIconLarge, &hicon, MAKELONG(0/*GetSystemMetrics(SM_CXICON)*/,GetSystemMetrics(SM_CXSMICON)));
759 if (SUCCEEDED(hr))
760 DestroyIcon(hIconLarge);
763 return hicon;
767 return 0;
771 static Entry* find_entry_shell(Entry* dir, LPCITEMIDLIST pidl)
773 Entry* entry;
775 for(entry=dir->down; entry; entry=entry->next) {
776 if (entry->pidl->mkid.cb == pidl->mkid.cb &&
777 !memcmp(entry->pidl, pidl, entry->pidl->mkid.cb))
778 return entry;
781 return 0;
784 static Entry* read_tree_shell(Root* root, LPITEMIDLIST pidl, SORT_ORDER sortOrder, HWND hwnd)
786 Entry* entry = &root->entry;
787 Entry* next;
788 LPITEMIDLIST next_pidl = pidl;
789 IShellFolder* folder;
790 IShellFolder* child = NULL;
791 HRESULT hr;
793 HCURSOR old_cursor = SetCursor(LoadCursorW(0, (LPCWSTR)IDC_WAIT));
795 entry->etype = ET_SHELL;
796 folder = Globals.iDesktop;
798 while(entry) {
799 entry->pidl = next_pidl;
800 entry->folder = folder;
802 if (!pidl->mkid.cb)
803 break;
805 /* copy first element of item idlist */
806 next_pidl = IMalloc_Alloc(Globals.iMalloc, pidl->mkid.cb+sizeof(USHORT));
807 memcpy(next_pidl, pidl, pidl->mkid.cb);
808 ((LPITEMIDLIST)((LPBYTE)next_pidl+pidl->mkid.cb))->mkid.cb = 0;
810 hr = IShellFolder_BindToObject(folder, next_pidl, 0, &IID_IShellFolder, (void**)&child);
811 if (FAILED(hr))
812 break;
814 read_directory(entry, NULL, sortOrder, hwnd);
816 if (entry->down)
817 entry->expanded = TRUE;
819 next = find_entry_shell(entry, next_pidl);
820 if (!next)
821 break;
823 folder = child;
824 entry = next;
826 /* go to next element */
827 pidl = (LPITEMIDLIST) ((LPBYTE)pidl+pidl->mkid.cb);
830 SetCursor(old_cursor);
832 return entry;
836 static void fill_w32fdata_shell(IShellFolder* folder, LPCITEMIDLIST pidl, SFGAOF attribs, WIN32_FIND_DATAW* w32fdata)
838 if (!(attribs & SFGAO_FILESYSTEM) ||
839 FAILED(SHGetDataFromIDListW(folder, pidl, SHGDFIL_FINDDATA, w32fdata, sizeof(WIN32_FIND_DATAW)))) {
840 WIN32_FILE_ATTRIBUTE_DATA fad;
841 IDataObject* pDataObj;
843 STGMEDIUM medium = {0, {0}, 0};
844 FORMATETC fmt = {Globals.cfStrFName, 0, DVASPECT_CONTENT, -1, TYMED_HGLOBAL};
846 HRESULT hr = IShellFolder_GetUIObjectOf(folder, 0, 1, &pidl, &IID_IDataObject, 0, (LPVOID*)&pDataObj);
848 if (SUCCEEDED(hr)) {
849 hr = IDataObject_GetData(pDataObj, &fmt, &medium);
851 IDataObject_Release(pDataObj);
853 if (SUCCEEDED(hr)) {
854 LPCWSTR path = GlobalLock(medium.UNION_MEMBER(hGlobal));
855 UINT sem_org = SetErrorMode(SEM_FAILCRITICALERRORS);
857 if (GetFileAttributesExW(path, GetFileExInfoStandard, &fad)) {
858 w32fdata->dwFileAttributes = fad.dwFileAttributes;
859 w32fdata->ftCreationTime = fad.ftCreationTime;
860 w32fdata->ftLastAccessTime = fad.ftLastAccessTime;
861 w32fdata->ftLastWriteTime = fad.ftLastWriteTime;
863 if (!(fad.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
864 w32fdata->nFileSizeLow = fad.nFileSizeLow;
865 w32fdata->nFileSizeHigh = fad.nFileSizeHigh;
869 SetErrorMode(sem_org);
871 GlobalUnlock(medium.UNION_MEMBER(hGlobal));
872 GlobalFree(medium.UNION_MEMBER(hGlobal));
877 if (attribs & (SFGAO_FOLDER|SFGAO_HASSUBFOLDER))
878 w32fdata->dwFileAttributes |= FILE_ATTRIBUTE_DIRECTORY;
880 if (attribs & SFGAO_READONLY)
881 w32fdata->dwFileAttributes |= FILE_ATTRIBUTE_READONLY;
883 if (attribs & SFGAO_COMPRESSED)
884 w32fdata->dwFileAttributes |= FILE_ATTRIBUTE_COMPRESSED;
888 static void read_directory_shell(Entry* dir, HWND hwnd)
890 IShellFolder* folder = dir->folder;
891 int level = dir->level + 1;
892 HRESULT hr;
894 IShellFolder* child;
895 IEnumIDList* idlist;
897 Entry* first_entry = NULL;
898 Entry* last = NULL;
899 Entry* entry;
901 if (!folder)
902 return;
904 hr = IShellFolder_EnumObjects(folder, hwnd, SHCONTF_FOLDERS|SHCONTF_NONFOLDERS|SHCONTF_INCLUDEHIDDEN|SHCONTF_SHAREABLE|SHCONTF_STORAGE, &idlist);
906 if (SUCCEEDED(hr)) {
907 for(;;) {
908 #define FETCH_ITEM_COUNT 32
909 LPITEMIDLIST pidls[FETCH_ITEM_COUNT];
910 SFGAOF attribs;
911 ULONG cnt = 0;
912 ULONG n;
914 memset(pidls, 0, sizeof(pidls));
916 hr = IEnumIDList_Next(idlist, FETCH_ITEM_COUNT, pidls, &cnt);
917 if (FAILED(hr))
918 break;
920 if (hr == S_FALSE)
921 break;
923 for(n=0; n<cnt; ++n) {
924 entry = alloc_entry();
926 if (!first_entry)
927 first_entry = entry;
929 if (last)
930 last->next = entry;
932 memset(&entry->data, 0, sizeof(WIN32_FIND_DATAW));
933 entry->bhfi_valid = FALSE;
935 attribs = ~SFGAO_FILESYSTEM; /*SFGAO_HASSUBFOLDER|SFGAO_FOLDER; SFGAO_FILESYSTEM sorgt dafür, daß "My Documents" anstatt von "Martin's Documents" angezeigt wird */
937 hr = IShellFolder_GetAttributesOf(folder, 1, (LPCITEMIDLIST*)&pidls[n], &attribs);
939 if (SUCCEEDED(hr)) {
940 if (attribs != (SFGAOF)~SFGAO_FILESYSTEM) {
941 fill_w32fdata_shell(folder, pidls[n], attribs, &entry->data);
943 entry->bhfi_valid = TRUE;
944 } else
945 attribs = 0;
946 } else
947 attribs = 0;
949 entry->pidl = pidls[n];
951 if (entry->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
952 hr = IShellFolder_BindToObject(folder, pidls[n], 0, &IID_IShellFolder, (void**)&child);
954 if (SUCCEEDED(hr))
955 entry->folder = child;
956 else
957 entry->folder = NULL;
959 else
960 entry->folder = NULL;
962 if (!entry->data.cFileName[0])
963 /*hr = */name_from_pidl(folder, pidls[n], entry->data.cFileName, MAX_PATH, /*SHGDN_INFOLDER*/0x2000/*0x2000=SHGDN_INCLUDE_NONFILESYS*/);
965 /* get display icons for files and virtual objects */
966 if (!(entry->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
967 !(attribs & SFGAO_FILESYSTEM)) {
968 entry->hicon = extract_icon(folder, pidls[n]);
970 if (!entry->hicon)
971 entry->hicon = (HICON)-1; /* don't try again later */
974 entry->down = NULL;
975 entry->up = dir;
976 entry->expanded = FALSE;
977 entry->scanned = FALSE;
978 entry->level = level;
980 entry->etype = ET_SHELL;
981 entry->bhfi_valid = FALSE;
983 last = entry;
987 IEnumIDList_Release(idlist);
990 if (last)
991 last->next = NULL;
993 dir->down = first_entry;
994 dir->scanned = TRUE;
997 /* sort order for different directory/file types */
998 enum TYPE_ORDER {
999 TO_DIR = 0,
1000 TO_DOT = 1,
1001 TO_DOTDOT = 2,
1002 TO_OTHER_DIR = 3,
1003 TO_FILE = 4
1006 /* distinguish between ".", ".." and any other directory names */
1007 static int TypeOrderFromDirname(LPCWSTR name)
1009 if (name[0] == '.') {
1010 if (name[1] == '\0')
1011 return TO_DOT; /* "." */
1013 if (name[1]=='.' && name[2]=='\0')
1014 return TO_DOTDOT; /* ".." */
1017 return TO_OTHER_DIR; /* anything else */
1020 /* directories first... */
1021 static int compareType(const WIN32_FIND_DATAW* fd1, const WIN32_FIND_DATAW* fd2)
1023 int order1 = fd1->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY? TO_DIR: TO_FILE;
1024 int order2 = fd2->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY? TO_DIR: TO_FILE;
1026 /* Handle "." and ".." as special case and move them at the very first beginning. */
1027 if (order1==TO_DIR && order2==TO_DIR) {
1028 order1 = TypeOrderFromDirname(fd1->cFileName);
1029 order2 = TypeOrderFromDirname(fd2->cFileName);
1032 return order2==order1? 0: order1<order2? -1: 1;
1036 static int compareName(const void* arg1, const void* arg2)
1038 const WIN32_FIND_DATAW* fd1 = &(*(const Entry* const*)arg1)->data;
1039 const WIN32_FIND_DATAW* fd2 = &(*(const Entry* const*)arg2)->data;
1041 int cmp = compareType(fd1, fd2);
1042 if (cmp)
1043 return cmp;
1045 return lstrcmpiW(fd1->cFileName, fd2->cFileName);
1048 static int compareExt(const void* arg1, const void* arg2)
1050 const WIN32_FIND_DATAW* fd1 = &(*(const Entry* const*)arg1)->data;
1051 const WIN32_FIND_DATAW* fd2 = &(*(const Entry* const*)arg2)->data;
1052 const WCHAR *name1, *name2, *ext1, *ext2;
1054 int cmp = compareType(fd1, fd2);
1055 if (cmp)
1056 return cmp;
1058 name1 = fd1->cFileName;
1059 name2 = fd2->cFileName;
1061 ext1 = strrchrW(name1, '.');
1062 ext2 = strrchrW(name2, '.');
1064 if (ext1)
1065 ext1++;
1066 else
1067 ext1 = sEmpty;
1069 if (ext2)
1070 ext2++;
1071 else
1072 ext2 = sEmpty;
1074 cmp = lstrcmpiW(ext1, ext2);
1075 if (cmp)
1076 return cmp;
1078 return lstrcmpiW(name1, name2);
1081 static int compareSize(const void* arg1, const void* arg2)
1083 const WIN32_FIND_DATAW* fd1 = &(*(const Entry* const*)arg1)->data;
1084 const WIN32_FIND_DATAW* fd2 = &(*(const Entry* const*)arg2)->data;
1086 int cmp = compareType(fd1, fd2);
1087 if (cmp)
1088 return cmp;
1090 cmp = fd2->nFileSizeHigh - fd1->nFileSizeHigh;
1092 if (cmp < 0)
1093 return -1;
1094 else if (cmp > 0)
1095 return 1;
1097 cmp = fd2->nFileSizeLow - fd1->nFileSizeLow;
1099 return cmp<0? -1: cmp>0? 1: 0;
1102 static int compareDate(const void* arg1, const void* arg2)
1104 const WIN32_FIND_DATAW* fd1 = &(*(const Entry* const*)arg1)->data;
1105 const WIN32_FIND_DATAW* fd2 = &(*(const Entry* const*)arg2)->data;
1107 int cmp = compareType(fd1, fd2);
1108 if (cmp)
1109 return cmp;
1111 return CompareFileTime(&fd2->ftLastWriteTime, &fd1->ftLastWriteTime);
1115 static int (*sortFunctions[])(const void* arg1, const void* arg2) = {
1116 compareName, /* SORT_NAME */
1117 compareExt, /* SORT_EXT */
1118 compareSize, /* SORT_SIZE */
1119 compareDate /* SORT_DATE */
1123 static void SortDirectory(Entry* dir, SORT_ORDER sortOrder)
1125 Entry* entry;
1126 Entry** array, **p;
1127 int len;
1129 len = 0;
1130 for(entry=dir->down; entry; entry=entry->next)
1131 len++;
1133 if (len) {
1134 array = HeapAlloc(GetProcessHeap(), 0, len*sizeof(Entry*));
1136 p = array;
1137 for(entry=dir->down; entry; entry=entry->next)
1138 *p++ = entry;
1140 /* call qsort with the appropriate compare function */
1141 qsort(array, len, sizeof(array[0]), sortFunctions[sortOrder]);
1143 dir->down = array[0];
1145 for(p=array; --len; p++)
1146 p[0]->next = p[1];
1148 (*p)->next = 0;
1150 HeapFree(GetProcessHeap(), 0, array);
1155 static void read_directory(Entry* dir, LPCWSTR path, SORT_ORDER sortOrder, HWND hwnd)
1157 WCHAR buffer[MAX_PATH];
1158 Entry* entry;
1159 LPCWSTR s;
1160 PWSTR d;
1162 if (dir->etype == ET_SHELL)
1164 read_directory_shell(dir, hwnd);
1166 if (Globals.prescan_node) {
1167 s = path;
1168 d = buffer;
1170 while(*s)
1171 *d++ = *s++;
1173 *d++ = '\\';
1175 for(entry=dir->down; entry; entry=entry->next)
1176 if (entry->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
1177 read_directory_shell(entry, hwnd);
1178 SortDirectory(entry, sortOrder);
1182 else
1183 #ifdef __WINE__
1184 if (dir->etype == ET_UNIX)
1186 read_directory_unix(dir, path);
1188 if (Globals.prescan_node) {
1189 s = path;
1190 d = buffer;
1192 while(*s)
1193 *d++ = *s++;
1195 *d++ = '/';
1197 for(entry=dir->down; entry; entry=entry->next)
1198 if (entry->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
1199 lstrcpyW(d, entry->data.cFileName);
1200 read_directory_unix(entry, buffer);
1201 SortDirectory(entry, sortOrder);
1205 else
1206 #endif
1208 read_directory_win(dir, path);
1210 if (Globals.prescan_node) {
1211 s = path;
1212 d = buffer;
1214 while(*s)
1215 *d++ = *s++;
1217 *d++ = '\\';
1219 for(entry=dir->down; entry; entry=entry->next)
1220 if (entry->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
1221 lstrcpyW(d, entry->data.cFileName);
1222 read_directory_win(entry, buffer);
1223 SortDirectory(entry, sortOrder);
1228 SortDirectory(dir, sortOrder);
1232 static Entry* read_tree(Root* root, LPCWSTR path, LPITEMIDLIST pidl, LPWSTR drv, SORT_ORDER sortOrder, HWND hwnd)
1234 #ifdef __WINE__
1235 static const WCHAR sSlash[] = {'/', '\0'};
1236 #endif
1237 static const WCHAR sBackslash[] = {'\\', '\0'};
1239 if (pidl)
1241 /* read shell namespace tree */
1242 root->drive_type = DRIVE_UNKNOWN;
1243 drv[0] = '\\';
1244 drv[1] = '\0';
1245 load_string(root->volname, sizeof(root->volname)/sizeof(root->volname[0]), IDS_DESKTOP);
1246 root->fs_flags = 0;
1247 load_string(root->fs, sizeof(root->fs)/sizeof(root->fs[0]), IDS_SHELL);
1249 return read_tree_shell(root, pidl, sortOrder, hwnd);
1251 else
1252 #ifdef __WINE__
1253 if (*path == '/')
1255 /* read unix file system tree */
1256 root->drive_type = GetDriveTypeW(path);
1258 lstrcatW(drv, sSlash);
1259 load_string(root->volname, sizeof(root->volname)/sizeof(root->volname[0]), IDS_ROOT_FS);
1260 root->fs_flags = 0;
1261 load_string(root->fs, sizeof(root->fs)/sizeof(root->fs[0]), IDS_UNIXFS);
1263 lstrcpyW(root->path, sSlash);
1265 return read_tree_unix(root, path, sortOrder, hwnd);
1267 #endif
1269 /* read WIN32 file system tree */
1270 root->drive_type = GetDriveTypeW(path);
1272 lstrcatW(drv, sBackslash);
1273 GetVolumeInformationW(drv, root->volname, _MAX_FNAME, 0, 0, &root->fs_flags, root->fs, _MAX_DIR);
1275 lstrcpyW(root->path, drv);
1277 return read_tree_win(root, path, sortOrder, hwnd);
1281 /* flags to filter different file types */
1282 enum TYPE_FILTER {
1283 TF_DIRECTORIES = 0x01,
1284 TF_PROGRAMS = 0x02,
1285 TF_DOCUMENTS = 0x04,
1286 TF_OTHERS = 0x08,
1287 TF_HIDDEN = 0x10,
1288 TF_ALL = 0x1F
1292 static ChildWnd* alloc_child_window(LPCWSTR path, LPITEMIDLIST pidl, HWND hwnd)
1294 WCHAR drv[_MAX_DRIVE+1], dir[_MAX_DIR], name[_MAX_FNAME], ext[_MAX_EXT];
1295 WCHAR dir_path[MAX_PATH];
1296 static const WCHAR sAsterics[] = {'*', '\0'};
1297 static const WCHAR sTitleFmt[] = {'%','s',' ','-',' ','%','s','\0'};
1299 ChildWnd* child = HeapAlloc(GetProcessHeap(), 0, sizeof(ChildWnd));
1300 Root* root = &child->root;
1301 Entry* entry;
1303 memset(child, 0, sizeof(ChildWnd));
1305 child->left.treePane = TRUE;
1306 child->left.visible_cols = 0;
1308 child->right.treePane = FALSE;
1309 child->right.visible_cols = COL_SIZE|COL_DATE|COL_TIME|COL_ATTRIBUTES|COL_INDEX|COL_LINKS;
1311 child->pos.length = sizeof(WINDOWPLACEMENT);
1312 child->pos.flags = 0;
1313 child->pos.showCmd = SW_SHOWNORMAL;
1314 child->pos.rcNormalPosition.left = CW_USEDEFAULT;
1315 child->pos.rcNormalPosition.top = CW_USEDEFAULT;
1316 child->pos.rcNormalPosition.right = CW_USEDEFAULT;
1317 child->pos.rcNormalPosition.bottom = CW_USEDEFAULT;
1319 child->focus_pane = 0;
1320 child->split_pos = DEFAULT_SPLIT_POS;
1321 child->sortOrder = SORT_NAME;
1322 child->header_wdths_ok = FALSE;
1324 if (path)
1326 int pathlen = strlenW(path);
1327 const WCHAR *npath = path;
1329 if (path[0] == '"' && path[pathlen - 1] == '"')
1331 npath++;
1332 pathlen--;
1334 lstrcpynW(child->path, npath, pathlen + 1);
1336 _wsplitpath(child->path, drv, dir, name, ext);
1339 lstrcpyW(child->filter_pattern, sAsterics);
1340 child->filter_flags = TF_ALL;
1342 root->entry.level = 0;
1344 lstrcpyW(dir_path, drv);
1345 lstrcatW(dir_path, dir);
1346 entry = read_tree(root, dir_path, pidl, drv, child->sortOrder, hwnd);
1348 if (root->entry.etype == ET_SHELL)
1349 load_string(root->entry.data.cFileName, sizeof(root->entry.data.cFileName)/sizeof(root->entry.data.cFileName[0]), IDS_DESKTOP);
1350 else
1351 wsprintfW(root->entry.data.cFileName, sTitleFmt, drv, root->fs);
1353 root->entry.data.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1355 child->left.root = &root->entry;
1356 child->right.root = NULL;
1358 set_curdir(child, entry, 0, hwnd);
1360 return child;
1364 /* free all memory associated with a child window */
1365 static void free_child_window(ChildWnd* child)
1367 free_entries(&child->root.entry);
1368 HeapFree(GetProcessHeap(), 0, child);
1372 /* get full path of specified directory entry */
1373 static void get_path(Entry* dir, PWSTR path)
1375 Entry* entry;
1376 int len = 0;
1377 int level = 0;
1379 if (dir->etype == ET_SHELL)
1381 SFGAOF attribs;
1382 HRESULT hr = S_OK;
1384 path[0] = '\0';
1386 attribs = 0;
1388 if (dir->folder)
1389 hr = IShellFolder_GetAttributesOf(dir->folder, 1, (LPCITEMIDLIST*)&dir->pidl, &attribs);
1391 if (SUCCEEDED(hr) && (attribs&SFGAO_FILESYSTEM)) {
1392 IShellFolder* parent = dir->up? dir->up->folder: Globals.iDesktop;
1394 hr = path_from_pidlW(parent, dir->pidl, path, MAX_PATH);
1397 else
1399 for(entry=dir; entry; level++) {
1400 LPCWSTR name;
1401 int l;
1404 LPCWSTR s;
1405 name = entry->data.cFileName;
1406 s = name;
1408 for(l=0; *s && *s != '/' && *s != '\\'; s++)
1409 l++;
1412 if (entry->up) {
1413 if (l > 0) {
1414 memmove(path+l+1, path, len*sizeof(WCHAR));
1415 memcpy(path+1, name, l*sizeof(WCHAR));
1416 len += l+1;
1418 if (entry->etype == ET_UNIX)
1419 path[0] = '/';
1420 else
1421 path[0] = '\\';
1424 entry = entry->up;
1425 } else {
1426 memmove(path+l, path, len*sizeof(WCHAR));
1427 memcpy(path, name, l*sizeof(WCHAR));
1428 len += l;
1429 break;
1433 if (!level) {
1434 if (entry->etype == ET_UNIX)
1435 path[len++] = '/';
1436 else
1437 path[len++] = '\\';
1440 path[len] = '\0';
1444 static windowOptions load_registry_settings(void)
1446 DWORD size;
1447 DWORD type;
1448 HKEY hKey;
1449 windowOptions opts;
1450 LOGFONTW logfont;
1452 RegOpenKeyExW( HKEY_CURRENT_USER, registry_key,
1453 0, KEY_QUERY_VALUE, &hKey );
1455 size = sizeof(DWORD);
1457 if( RegQueryValueExW( hKey, reg_start_x, NULL, &type,
1458 (LPBYTE) &opts.start_x, &size ) != ERROR_SUCCESS )
1459 opts.start_x = CW_USEDEFAULT;
1461 if( RegQueryValueExW( hKey, reg_start_y, NULL, &type,
1462 (LPBYTE) &opts.start_y, &size ) != ERROR_SUCCESS )
1463 opts.start_y = CW_USEDEFAULT;
1465 if( RegQueryValueExW( hKey, reg_width, NULL, &type,
1466 (LPBYTE) &opts.width, &size ) != ERROR_SUCCESS )
1467 opts.width = CW_USEDEFAULT;
1469 if( RegQueryValueExW( hKey, reg_height, NULL, &type,
1470 (LPBYTE) &opts.height, &size ) != ERROR_SUCCESS )
1471 opts.height = CW_USEDEFAULT;
1472 size=sizeof(logfont);
1473 if( RegQueryValueExW( hKey, reg_logfont, NULL, &type,
1474 (LPBYTE) &logfont, &size ) != ERROR_SUCCESS )
1475 GetObjectW(GetStockObject(DEFAULT_GUI_FONT),sizeof(logfont),&logfont);
1477 RegCloseKey( hKey );
1479 Globals.hfont = CreateFontIndirectW(&logfont);
1480 return opts;
1483 static void save_registry_settings(void)
1485 WINDOWINFO wi;
1486 HKEY hKey;
1487 INT width, height;
1488 LOGFONTW logfont;
1490 wi.cbSize = sizeof( WINDOWINFO );
1491 GetWindowInfo(Globals.hMainWnd, &wi);
1492 width = wi.rcWindow.right - wi.rcWindow.left;
1493 height = wi.rcWindow.bottom - wi.rcWindow.top;
1495 if ( RegOpenKeyExW( HKEY_CURRENT_USER, registry_key,
1496 0, KEY_SET_VALUE, &hKey ) != ERROR_SUCCESS )
1498 /* Unable to save registry settings - try to create key */
1499 if ( RegCreateKeyExW( HKEY_CURRENT_USER, registry_key,
1500 0, NULL, REG_OPTION_NON_VOLATILE,
1501 KEY_SET_VALUE, NULL, &hKey, NULL ) != ERROR_SUCCESS )
1503 /* FIXME: Cannot create key */
1504 return;
1507 /* Save all of the settings */
1508 RegSetValueExW( hKey, reg_start_x, 0, REG_DWORD,
1509 (LPBYTE) &wi.rcWindow.left, sizeof(DWORD) );
1510 RegSetValueExW( hKey, reg_start_y, 0, REG_DWORD,
1511 (LPBYTE) &wi.rcWindow.top, sizeof(DWORD) );
1512 RegSetValueExW( hKey, reg_width, 0, REG_DWORD,
1513 (LPBYTE) &width, sizeof(DWORD) );
1514 RegSetValueExW( hKey, reg_height, 0, REG_DWORD,
1515 (LPBYTE) &height, sizeof(DWORD) );
1516 GetObjectW(Globals.hfont, sizeof(logfont), &logfont);
1517 RegSetValueExW( hKey, reg_logfont, 0, REG_BINARY,
1518 (LPBYTE)&logfont, sizeof(LOGFONTW) );
1520 /* TODO: Save more settings here (List vs. Detailed View, etc.) */
1521 RegCloseKey( hKey );
1524 static void resize_frame_rect(HWND hwnd, PRECT prect)
1526 int new_top;
1527 RECT rt;
1529 if (IsWindowVisible(Globals.htoolbar)) {
1530 SendMessageW(Globals.htoolbar, WM_SIZE, 0, 0);
1531 GetClientRect(Globals.htoolbar, &rt);
1532 prect->top = rt.bottom+3;
1533 prect->bottom -= rt.bottom+3;
1536 if (IsWindowVisible(Globals.hdrivebar)) {
1537 SendMessageW(Globals.hdrivebar, WM_SIZE, 0, 0);
1538 GetClientRect(Globals.hdrivebar, &rt);
1539 new_top = --prect->top + rt.bottom+3;
1540 MoveWindow(Globals.hdrivebar, 0, prect->top, rt.right, new_top, TRUE);
1541 prect->top = new_top;
1542 prect->bottom -= rt.bottom+2;
1545 if (IsWindowVisible(Globals.hstatusbar)) {
1546 int parts[] = {300, 500};
1548 SendMessageW(Globals.hstatusbar, WM_SIZE, 0, 0);
1549 SendMessageW(Globals.hstatusbar, SB_SETPARTS, 2, (LPARAM)&parts);
1550 GetClientRect(Globals.hstatusbar, &rt);
1551 prect->bottom -= rt.bottom;
1554 MoveWindow(Globals.hmdiclient, prect->left-1,prect->top-1,prect->right+2,prect->bottom+1, TRUE);
1557 static void resize_frame(HWND hwnd, int cx, int cy)
1559 RECT rect;
1561 rect.left = 0;
1562 rect.top = 0;
1563 rect.right = cx;
1564 rect.bottom = cy;
1566 resize_frame_rect(hwnd, &rect);
1569 static void resize_frame_client(HWND hwnd)
1571 RECT rect;
1573 GetClientRect(hwnd, &rect);
1575 resize_frame_rect(hwnd, &rect);
1579 static HHOOK hcbthook;
1580 static ChildWnd* newchild = NULL;
1582 static LRESULT CALLBACK CBTProc(int code, WPARAM wparam, LPARAM lparam)
1584 if (code==HCBT_CREATEWND && newchild) {
1585 ChildWnd* child = newchild;
1586 newchild = NULL;
1588 child->hwnd = (HWND) wparam;
1589 SetWindowLongPtrW(child->hwnd, GWLP_USERDATA, (LPARAM)child);
1592 return CallNextHookEx(hcbthook, code, wparam, lparam);
1595 static HWND create_child_window(ChildWnd* child)
1597 MDICREATESTRUCTW mcs;
1598 int idx;
1600 mcs.szClass = sWINEFILETREE;
1601 mcs.szTitle = child->path;
1602 mcs.hOwner = Globals.hInstance;
1603 mcs.x = child->pos.rcNormalPosition.left;
1604 mcs.y = child->pos.rcNormalPosition.top;
1605 mcs.cx = child->pos.rcNormalPosition.right-child->pos.rcNormalPosition.left;
1606 mcs.cy = child->pos.rcNormalPosition.bottom-child->pos.rcNormalPosition.top;
1607 mcs.style = 0;
1608 mcs.lParam = 0;
1610 hcbthook = SetWindowsHookExW(WH_CBT, CBTProc, 0, GetCurrentThreadId());
1612 newchild = child;
1613 child->hwnd = (HWND)SendMessageW(Globals.hmdiclient, WM_MDICREATE, 0, (LPARAM)&mcs);
1614 if (!child->hwnd) {
1615 UnhookWindowsHookEx(hcbthook);
1616 return 0;
1619 UnhookWindowsHookEx(hcbthook);
1621 SendMessageW(child->left.hwnd, LB_SETITEMHEIGHT, 1, max(Globals.spaceSize.cy,IMAGE_HEIGHT+3));
1622 SendMessageW(child->right.hwnd, LB_SETITEMHEIGHT, 1, max(Globals.spaceSize.cy,IMAGE_HEIGHT+3));
1624 idx = SendMessageW(child->left.hwnd, LB_FINDSTRING, 0, (LPARAM)child->left.cur);
1625 SendMessageW(child->left.hwnd, LB_SETCURSEL, idx, 0);
1627 return child->hwnd;
1630 #define RFF_NODEFAULT 0x02 /* No default item selected. */
1632 static void WineFile_OnRun( HWND hwnd )
1634 static const WCHAR shell32_dll[] = {'S','H','E','L','L','3','2','.','D','L','L',0};
1635 void (WINAPI *pRunFileDlgAW )(HWND, HICON, LPWSTR, LPWSTR, LPWSTR, DWORD);
1636 HMODULE hshell = GetModuleHandleW( shell32_dll );
1637 HICON hIcon = LoadIconW(Globals.hInstance, MAKEINTRESOURCEW(IDI_WINEFILE));
1639 pRunFileDlgAW = (void*)GetProcAddress(hshell, (LPCSTR)61);
1640 if (pRunFileDlgAW) pRunFileDlgAW( hwnd, hIcon, NULL, NULL, NULL, RFF_NODEFAULT);
1643 static INT_PTR CALLBACK DestinationDlgProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam)
1645 WCHAR b1[BUFFER_LEN], b2[BUFFER_LEN];
1647 switch(nmsg) {
1648 case WM_INITDIALOG:
1649 SetWindowLongPtrW(hwnd, GWLP_USERDATA, lparam);
1650 SetWindowTextW(GetDlgItem(hwnd, 201), (LPCWSTR)lparam);
1651 return 1;
1653 case WM_COMMAND: {
1654 int id = (int)wparam;
1656 switch(id) {
1657 case IDOK: {
1658 LPWSTR dest = (LPWSTR)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
1659 GetWindowTextW(GetDlgItem(hwnd, 201), dest, MAX_PATH);
1660 EndDialog(hwnd, id);
1661 break;}
1663 case IDCANCEL:
1664 EndDialog(hwnd, id);
1665 break;
1667 case 254:
1668 MessageBoxW(hwnd, RS(b1,IDS_NO_IMPL), RS(b2,IDS_WINEFILE), MB_OK);
1669 break;
1672 return 1;
1676 return 0;
1680 struct FilterDialog {
1681 WCHAR pattern[MAX_PATH];
1682 int flags;
1685 static INT_PTR CALLBACK FilterDialogDlgProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam)
1687 static struct FilterDialog* dlg;
1689 switch(nmsg) {
1690 case WM_INITDIALOG:
1691 dlg = (struct FilterDialog*) lparam;
1692 SetWindowTextW(GetDlgItem(hwnd, IDC_VIEW_PATTERN), dlg->pattern);
1693 set_check(hwnd, IDC_VIEW_TYPE_DIRECTORIES, dlg->flags&TF_DIRECTORIES);
1694 set_check(hwnd, IDC_VIEW_TYPE_PROGRAMS, dlg->flags&TF_PROGRAMS);
1695 set_check(hwnd, IDC_VIEW_TYPE_DOCUMENTS, dlg->flags&TF_DOCUMENTS);
1696 set_check(hwnd, IDC_VIEW_TYPE_OTHERS, dlg->flags&TF_OTHERS);
1697 set_check(hwnd, IDC_VIEW_TYPE_HIDDEN, dlg->flags&TF_HIDDEN);
1698 return 1;
1700 case WM_COMMAND: {
1701 int id = (int)wparam;
1703 if (id == IDOK) {
1704 int flags = 0;
1706 GetWindowTextW(GetDlgItem(hwnd, IDC_VIEW_PATTERN), dlg->pattern, MAX_PATH);
1708 flags |= get_check(hwnd, IDC_VIEW_TYPE_DIRECTORIES) ? TF_DIRECTORIES : 0;
1709 flags |= get_check(hwnd, IDC_VIEW_TYPE_PROGRAMS) ? TF_PROGRAMS : 0;
1710 flags |= get_check(hwnd, IDC_VIEW_TYPE_DOCUMENTS) ? TF_DOCUMENTS : 0;
1711 flags |= get_check(hwnd, IDC_VIEW_TYPE_OTHERS) ? TF_OTHERS : 0;
1712 flags |= get_check(hwnd, IDC_VIEW_TYPE_HIDDEN) ? TF_HIDDEN : 0;
1714 dlg->flags = flags;
1716 EndDialog(hwnd, id);
1717 } else if (id == IDCANCEL)
1718 EndDialog(hwnd, id);
1720 return 1;}
1723 return 0;
1727 struct PropertiesDialog {
1728 WCHAR path[MAX_PATH];
1729 Entry entry;
1730 void* pVersionData;
1733 /* Structure used to store enumerated languages and code pages. */
1734 struct LANGANDCODEPAGE {
1735 WORD wLanguage;
1736 WORD wCodePage;
1737 } *lpTranslate;
1739 static LPCSTR InfoStrings[] = {
1740 "Comments",
1741 "CompanyName",
1742 "FileDescription",
1743 "FileVersion",
1744 "InternalName",
1745 "LegalCopyright",
1746 "LegalTrademarks",
1747 "OriginalFilename",
1748 "PrivateBuild",
1749 "ProductName",
1750 "ProductVersion",
1751 "SpecialBuild",
1752 NULL
1755 static void PropDlg_DisplayValue(HWND hlbox, HWND hedit)
1757 int idx = SendMessageW(hlbox, LB_GETCURSEL, 0, 0);
1759 if (idx != LB_ERR) {
1760 LPCWSTR pValue = (LPCWSTR)SendMessageW(hlbox, LB_GETITEMDATA, idx, 0);
1762 if (pValue)
1763 SetWindowTextW(hedit, pValue);
1767 static void CheckForFileInfo(struct PropertiesDialog* dlg, HWND hwnd, LPCWSTR strFilename)
1769 static const WCHAR sBackSlash[] = {'\\','\0'};
1770 static const WCHAR sTranslation[] = {'\\','V','a','r','F','i','l','e','I','n','f','o','\\','T','r','a','n','s','l','a','t','i','o','n','\0'};
1771 static const WCHAR sStringFileInfo[] = {'\\','S','t','r','i','n','g','F','i','l','e','I','n','f','o','\\',
1772 '%','0','4','x','%','0','4','x','\\','%','s','\0'};
1773 static const WCHAR sFmt[] = {'%','d','.','%','d','.','%','d','.','%','d','\0'};
1774 DWORD dwVersionDataLen = GetFileVersionInfoSizeW(strFilename, NULL);
1776 if (dwVersionDataLen) {
1777 dlg->pVersionData = HeapAlloc(GetProcessHeap(), 0, dwVersionDataLen);
1779 if (GetFileVersionInfoW(strFilename, 0, dwVersionDataLen, dlg->pVersionData)) {
1780 LPVOID pVal;
1781 UINT nValLen;
1783 if (VerQueryValueW(dlg->pVersionData, sBackSlash, &pVal, &nValLen)) {
1784 if (nValLen == sizeof(VS_FIXEDFILEINFO)) {
1785 VS_FIXEDFILEINFO* pFixedFileInfo = (VS_FIXEDFILEINFO*)pVal;
1786 WCHAR buffer[BUFFER_LEN];
1788 sprintfW(buffer, sFmt,
1789 HIWORD(pFixedFileInfo->dwFileVersionMS), LOWORD(pFixedFileInfo->dwFileVersionMS),
1790 HIWORD(pFixedFileInfo->dwFileVersionLS), LOWORD(pFixedFileInfo->dwFileVersionLS));
1792 SetDlgItemTextW(hwnd, IDC_STATIC_PROP_VERSION, buffer);
1796 /* Read the list of languages and code pages. */
1797 if (VerQueryValueW(dlg->pVersionData, sTranslation, &pVal, &nValLen)) {
1798 struct LANGANDCODEPAGE* pTranslate = (struct LANGANDCODEPAGE*)pVal;
1799 struct LANGANDCODEPAGE* pEnd = (struct LANGANDCODEPAGE*)((LPBYTE)pVal+nValLen);
1801 HWND hlbox = GetDlgItem(hwnd, IDC_LIST_PROP_VERSION_TYPES);
1803 /* Read the file description for each language and code page. */
1804 for(; pTranslate<pEnd; ++pTranslate) {
1805 LPCSTR* p;
1807 for(p=InfoStrings; *p; ++p) {
1808 WCHAR subblock[200];
1809 WCHAR infoStr[100];
1810 LPCWSTR pTxt;
1811 UINT nValLen;
1813 LPCSTR pInfoString = *p;
1814 MultiByteToWideChar(CP_ACP, 0, pInfoString, -1, infoStr, 100);
1815 wsprintfW(subblock, sStringFileInfo, pTranslate->wLanguage, pTranslate->wCodePage, infoStr);
1817 /* Retrieve file description for language and code page */
1818 if (VerQueryValueW(dlg->pVersionData, subblock, (PVOID)&pTxt, &nValLen)) {
1819 int idx = SendMessageW(hlbox, LB_ADDSTRING, 0L, (LPARAM)infoStr);
1820 SendMessageW(hlbox, LB_SETITEMDATA, idx, (LPARAM)pTxt);
1825 SendMessageW(hlbox, LB_SETCURSEL, 0, 0);
1827 PropDlg_DisplayValue(hlbox, GetDlgItem(hwnd,IDC_LIST_PROP_VERSION_VALUES));
1833 static INT_PTR CALLBACK PropertiesDialogDlgProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam)
1835 static struct PropertiesDialog* dlg;
1837 switch(nmsg) {
1838 case WM_INITDIALOG: {
1839 static const WCHAR sByteFmt[] = {'%','s',' ','B','y','t','e','s','\0'};
1840 WCHAR b1[BUFFER_LEN], b2[BUFFER_LEN];
1841 LPWIN32_FIND_DATAW pWFD;
1843 dlg = (struct PropertiesDialog*) lparam;
1844 pWFD = (LPWIN32_FIND_DATAW)&dlg->entry.data;
1846 GetWindowTextW(hwnd, b1, MAX_PATH);
1847 wsprintfW(b2, b1, pWFD->cFileName);
1848 SetWindowTextW(hwnd, b2);
1850 format_date(&pWFD->ftLastWriteTime, b1, COL_DATE|COL_TIME);
1851 SetWindowTextW(GetDlgItem(hwnd, IDC_STATIC_PROP_LASTCHANGE), b1);
1853 format_longlong( b1, ((ULONGLONG)pWFD->nFileSizeHigh << 32) | pWFD->nFileSizeLow );
1854 wsprintfW(b2, sByteFmt, b1);
1855 SetWindowTextW(GetDlgItem(hwnd, IDC_STATIC_PROP_SIZE), b2);
1857 SetWindowTextW(GetDlgItem(hwnd, IDC_STATIC_PROP_FILENAME), pWFD->cFileName);
1858 SetWindowTextW(GetDlgItem(hwnd, IDC_STATIC_PROP_PATH), dlg->path);
1860 set_check(hwnd, IDC_CHECK_READONLY, pWFD->dwFileAttributes&FILE_ATTRIBUTE_READONLY);
1861 set_check(hwnd, IDC_CHECK_ARCHIVE, pWFD->dwFileAttributes&FILE_ATTRIBUTE_ARCHIVE);
1862 set_check(hwnd, IDC_CHECK_COMPRESSED, pWFD->dwFileAttributes&FILE_ATTRIBUTE_COMPRESSED);
1863 set_check(hwnd, IDC_CHECK_HIDDEN, pWFD->dwFileAttributes&FILE_ATTRIBUTE_HIDDEN);
1864 set_check(hwnd, IDC_CHECK_SYSTEM, pWFD->dwFileAttributes&FILE_ATTRIBUTE_SYSTEM);
1866 CheckForFileInfo(dlg, hwnd, dlg->path);
1867 return 1;}
1869 case WM_COMMAND: {
1870 int id = (int)wparam;
1872 switch(HIWORD(wparam)) {
1873 case LBN_SELCHANGE: {
1874 HWND hlbox = GetDlgItem(hwnd, IDC_LIST_PROP_VERSION_TYPES);
1875 PropDlg_DisplayValue(hlbox, GetDlgItem(hwnd,IDC_LIST_PROP_VERSION_VALUES));
1876 break;
1879 case BN_CLICKED:
1880 if (id==IDOK || id==IDCANCEL)
1881 EndDialog(hwnd, id);
1884 return 1;}
1886 case WM_NCDESTROY:
1887 HeapFree(GetProcessHeap(), 0, dlg->pVersionData);
1888 dlg->pVersionData = NULL;
1889 break;
1892 return 0;
1895 static void show_properties_dlg(Entry* entry, HWND hwnd)
1897 struct PropertiesDialog dlg;
1899 memset(&dlg, 0, sizeof(struct PropertiesDialog));
1900 get_path(entry, dlg.path);
1901 memcpy(&dlg.entry, entry, sizeof(Entry));
1903 DialogBoxParamW(Globals.hInstance, MAKEINTRESOURCEW(IDD_DIALOG_PROPERTIES), hwnd, PropertiesDialogDlgProc, (LPARAM)&dlg);
1906 static struct FullScreenParameters {
1907 BOOL mode;
1908 RECT orgPos;
1909 BOOL wasZoomed;
1910 } g_fullscreen = {
1911 FALSE, /* mode */
1912 {0, 0, 0, 0},
1913 FALSE
1916 static void frame_get_clientspace(HWND hwnd, PRECT prect)
1918 RECT rt;
1920 if (!IsIconic(hwnd))
1921 GetClientRect(hwnd, prect);
1922 else {
1923 WINDOWPLACEMENT wp;
1925 GetWindowPlacement(hwnd, &wp);
1927 prect->left = prect->top = 0;
1928 prect->right = wp.rcNormalPosition.right-wp.rcNormalPosition.left-
1929 2*(GetSystemMetrics(SM_CXSIZEFRAME)+GetSystemMetrics(SM_CXEDGE));
1930 prect->bottom = wp.rcNormalPosition.bottom-wp.rcNormalPosition.top-
1931 2*(GetSystemMetrics(SM_CYSIZEFRAME)+GetSystemMetrics(SM_CYEDGE))-
1932 GetSystemMetrics(SM_CYCAPTION)-GetSystemMetrics(SM_CYMENUSIZE);
1935 if (IsWindowVisible(Globals.htoolbar)) {
1936 GetClientRect(Globals.htoolbar, &rt);
1937 prect->top += rt.bottom+2;
1940 if (IsWindowVisible(Globals.hdrivebar)) {
1941 GetClientRect(Globals.hdrivebar, &rt);
1942 prect->top += rt.bottom+2;
1945 if (IsWindowVisible(Globals.hstatusbar)) {
1946 GetClientRect(Globals.hstatusbar, &rt);
1947 prect->bottom -= rt.bottom;
1951 static BOOL toggle_fullscreen(HWND hwnd)
1953 RECT rt;
1955 if ((g_fullscreen.mode=!g_fullscreen.mode)) {
1956 GetWindowRect(hwnd, &g_fullscreen.orgPos);
1957 g_fullscreen.wasZoomed = IsZoomed(hwnd);
1959 Frame_CalcFrameClient(hwnd, &rt);
1960 MapWindowPoints( hwnd, 0, (POINT *)&rt, 2 );
1962 rt.left = g_fullscreen.orgPos.left-rt.left;
1963 rt.top = g_fullscreen.orgPos.top-rt.top;
1964 rt.right = GetSystemMetrics(SM_CXSCREEN)+g_fullscreen.orgPos.right-rt.right;
1965 rt.bottom = GetSystemMetrics(SM_CYSCREEN)+g_fullscreen.orgPos.bottom-rt.bottom;
1967 MoveWindow(hwnd, rt.left, rt.top, rt.right-rt.left, rt.bottom-rt.top, TRUE);
1968 } else {
1969 MoveWindow(hwnd, g_fullscreen.orgPos.left, g_fullscreen.orgPos.top,
1970 g_fullscreen.orgPos.right-g_fullscreen.orgPos.left,
1971 g_fullscreen.orgPos.bottom-g_fullscreen.orgPos.top, TRUE);
1973 if (g_fullscreen.wasZoomed)
1974 ShowWindow(hwnd, WS_MAXIMIZE);
1977 return g_fullscreen.mode;
1980 static void fullscreen_move(HWND hwnd)
1982 RECT rt, pos;
1983 GetWindowRect(hwnd, &pos);
1985 Frame_CalcFrameClient(hwnd, &rt);
1986 MapWindowPoints( hwnd, 0, (POINT *)&rt, 2 );
1988 rt.left = pos.left-rt.left;
1989 rt.top = pos.top-rt.top;
1990 rt.right = GetSystemMetrics(SM_CXSCREEN)+pos.right-rt.right;
1991 rt.bottom = GetSystemMetrics(SM_CYSCREEN)+pos.bottom-rt.bottom;
1993 MoveWindow(hwnd, rt.left, rt.top, rt.right-rt.left, rt.bottom-rt.top, TRUE);
1996 static void toggle_child(HWND hwnd, UINT cmd, HWND hchild)
1998 BOOL vis = IsWindowVisible(hchild);
2000 CheckMenuItem(Globals.hMenuOptions, cmd, vis?MF_BYCOMMAND:MF_BYCOMMAND|MF_CHECKED);
2002 ShowWindow(hchild, vis?SW_HIDE:SW_SHOW);
2004 if (g_fullscreen.mode)
2005 fullscreen_move(hwnd);
2007 resize_frame_client(hwnd);
2010 static BOOL activate_drive_window(LPCWSTR path)
2012 WCHAR drv1[_MAX_DRIVE], drv2[_MAX_DRIVE];
2013 HWND child_wnd;
2015 _wsplitpath(path, drv1, 0, 0, 0);
2017 /* search for an already open window for the same drive */
2018 for(child_wnd=GetNextWindow(Globals.hmdiclient,GW_CHILD); child_wnd; child_wnd=GetNextWindow(child_wnd, GW_HWNDNEXT)) {
2019 ChildWnd* child = (ChildWnd*)GetWindowLongPtrW(child_wnd, GWLP_USERDATA);
2021 if (child) {
2022 _wsplitpath(child->root.path, drv2, 0, 0, 0);
2024 if (!lstrcmpiW(drv2, drv1)) {
2025 SendMessageW(Globals.hmdiclient, WM_MDIACTIVATE, (WPARAM)child_wnd, 0);
2027 if (IsIconic(child_wnd))
2028 ShowWindow(child_wnd, SW_SHOWNORMAL);
2030 return TRUE;
2035 return FALSE;
2038 static BOOL activate_fs_window(LPCWSTR filesys)
2040 HWND child_wnd;
2042 /* search for an already open window of the given file system name */
2043 for(child_wnd=GetNextWindow(Globals.hmdiclient,GW_CHILD); child_wnd; child_wnd=GetNextWindow(child_wnd, GW_HWNDNEXT)) {
2044 ChildWnd* child = (ChildWnd*) GetWindowLongPtrW(child_wnd, GWLP_USERDATA);
2046 if (child) {
2047 if (!lstrcmpiW(child->root.fs, filesys)) {
2048 SendMessageW(Globals.hmdiclient, WM_MDIACTIVATE, (WPARAM)child_wnd, 0);
2050 if (IsIconic(child_wnd))
2051 ShowWindow(child_wnd, SW_SHOWNORMAL);
2053 return TRUE;
2058 return FALSE;
2061 static LRESULT CALLBACK FrameWndProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam)
2063 WCHAR b1[BUFFER_LEN], b2[BUFFER_LEN];
2065 switch(nmsg) {
2066 case WM_CLOSE:
2067 if (Globals.saveSettings)
2068 save_registry_settings();
2070 DestroyWindow(hwnd);
2072 /* clear handle variables */
2073 Globals.hMenuFrame = 0;
2074 Globals.hMenuView = 0;
2075 Globals.hMenuOptions = 0;
2076 Globals.hMainWnd = 0;
2077 Globals.hmdiclient = 0;
2078 Globals.hdrivebar = 0;
2079 break;
2081 case WM_DESTROY:
2082 PostQuitMessage(0);
2083 break;
2085 case WM_INITMENUPOPUP: {
2086 HWND hwndClient = (HWND)SendMessageW(Globals.hmdiclient, WM_MDIGETACTIVE, 0, 0);
2088 if (!SendMessageW(hwndClient, WM_INITMENUPOPUP, wparam, lparam))
2089 return 0;
2090 break;}
2092 case WM_COMMAND: {
2093 UINT cmd = LOWORD(wparam);
2094 HWND hwndClient = (HWND)SendMessageW(Globals.hmdiclient, WM_MDIGETACTIVE, 0, 0);
2096 if (SendMessageW(hwndClient, WM_DISPATCH_COMMAND, wparam, lparam))
2097 break;
2099 if (cmd>=ID_DRIVE_FIRST && cmd<=ID_DRIVE_FIRST+0xFF) {
2100 WCHAR drv[_MAX_DRIVE], path[MAX_PATH];
2101 ChildWnd* child;
2102 LPCWSTR root = Globals.drives;
2103 int i;
2105 for(i=cmd-ID_DRIVE_FIRST; i--; root++)
2106 while(*root)
2107 root++;
2109 if (activate_drive_window(root))
2110 return 0;
2112 _wsplitpath(root, drv, 0, 0, 0);
2114 if (!SetCurrentDirectoryW(drv)) {
2115 display_error(hwnd, GetLastError());
2116 return 0;
2119 GetCurrentDirectoryW(MAX_PATH, path); /*TODO: store last directory per drive */
2120 child = alloc_child_window(path, NULL, hwnd);
2122 if (!create_child_window(child))
2123 HeapFree(GetProcessHeap(), 0, child);
2124 } else switch(cmd) {
2125 case ID_FILE_EXIT:
2126 SendMessageW(hwnd, WM_CLOSE, 0, 0);
2127 break;
2129 case ID_WINDOW_NEW: {
2130 WCHAR path[MAX_PATH];
2131 ChildWnd* child;
2133 GetCurrentDirectoryW(MAX_PATH, path);
2134 child = alloc_child_window(path, NULL, hwnd);
2136 if (!create_child_window(child))
2137 HeapFree(GetProcessHeap(), 0, child);
2138 break;}
2140 case ID_REFRESH:
2141 refresh_drives();
2142 break;
2144 case ID_WINDOW_CASCADE:
2145 SendMessageW(Globals.hmdiclient, WM_MDICASCADE, 0, 0);
2146 break;
2148 case ID_WINDOW_TILE_HORZ:
2149 SendMessageW(Globals.hmdiclient, WM_MDITILE, MDITILE_HORIZONTAL, 0);
2150 break;
2152 case ID_WINDOW_TILE_VERT:
2153 SendMessageW(Globals.hmdiclient, WM_MDITILE, MDITILE_VERTICAL, 0);
2154 break;
2156 case ID_WINDOW_ARRANGE:
2157 SendMessageW(Globals.hmdiclient, WM_MDIICONARRANGE, 0, 0);
2158 break;
2160 case ID_SELECT_FONT:
2161 choose_font(hwnd);
2162 break;
2164 case ID_VIEW_TOOL_BAR:
2165 toggle_child(hwnd, cmd, Globals.htoolbar);
2166 break;
2168 case ID_VIEW_DRIVE_BAR:
2169 toggle_child(hwnd, cmd, Globals.hdrivebar);
2170 break;
2172 case ID_VIEW_STATUSBAR:
2173 toggle_child(hwnd, cmd, Globals.hstatusbar);
2174 break;
2176 case ID_VIEW_SAVESETTINGS:
2177 Globals.saveSettings = !Globals.saveSettings;
2178 CheckMenuItem(Globals.hMenuOptions, ID_VIEW_SAVESETTINGS,
2179 Globals.saveSettings ? MF_CHECKED : MF_UNCHECKED );
2180 break;
2182 case ID_RUN:
2183 WineFile_OnRun( hwnd );
2184 break;
2186 case ID_CONNECT_NETWORK_DRIVE: {
2187 DWORD ret = WNetConnectionDialog(hwnd, RESOURCETYPE_DISK);
2188 if (ret == NO_ERROR)
2189 refresh_drives();
2190 else if (ret != (DWORD)-1) {
2191 if (ret == ERROR_EXTENDED_ERROR)
2192 display_network_error(hwnd);
2193 else
2194 display_error(hwnd, ret);
2196 break;}
2198 case ID_DISCONNECT_NETWORK_DRIVE: {
2199 DWORD ret = WNetDisconnectDialog(hwnd, RESOURCETYPE_DISK);
2200 if (ret == NO_ERROR)
2201 refresh_drives();
2202 else if (ret != (DWORD)-1) {
2203 if (ret == ERROR_EXTENDED_ERROR)
2204 display_network_error(hwnd);
2205 else
2206 display_error(hwnd, ret);
2208 break;}
2210 case ID_HELP:
2211 WinHelpW(hwnd, RS(b1,IDS_WINEFILE), HELP_INDEX, 0);
2212 break;
2214 case ID_VIEW_FULLSCREEN:
2215 CheckMenuItem(Globals.hMenuOptions, cmd, toggle_fullscreen(hwnd)?MF_CHECKED:0);
2216 break;
2218 #ifdef __WINE__
2219 case ID_DRIVE_UNIX_FS: {
2220 WCHAR path[MAX_PATH];
2221 char cpath[MAX_PATH];
2222 ChildWnd* child;
2224 if (activate_fs_window(RS(b1,IDS_UNIXFS)))
2225 break;
2227 getcwd(cpath, MAX_PATH);
2228 MultiByteToWideChar(CP_UNIXCP, 0, cpath, -1, path, MAX_PATH);
2229 child = alloc_child_window(path, NULL, hwnd);
2231 if (!create_child_window(child))
2232 HeapFree(GetProcessHeap(), 0, child);
2233 break;}
2234 #endif
2235 case ID_DRIVE_SHELL_NS: {
2236 WCHAR path[MAX_PATH];
2237 ChildWnd* child;
2239 if (activate_fs_window(RS(b1,IDS_SHELL)))
2240 break;
2242 GetCurrentDirectoryW(MAX_PATH, path);
2243 child = alloc_child_window(path, get_path_pidl(path,hwnd), hwnd);
2245 if (!create_child_window(child))
2246 HeapFree(GetProcessHeap(), 0, child);
2247 break;}
2249 /*TODO: There are even more menu items! */
2251 case ID_ABOUT:
2252 ShellAboutW(hwnd, RS(b1,IDS_WINEFILE), NULL,
2253 LoadImageW( Globals.hInstance, MAKEINTRESOURCEW(IDI_WINEFILE),
2254 IMAGE_ICON, 48, 48, LR_SHARED ));
2255 break;
2257 default:
2258 /*TODO: if (wParam >= PM_FIRST_LANGUAGE && wParam <= PM_LAST_LANGUAGE)
2259 STRING_SelectLanguageByNumber(wParam - PM_FIRST_LANGUAGE);
2260 else */if ((cmd<IDW_FIRST_CHILD || cmd>=IDW_FIRST_CHILD+0x100) &&
2261 (cmd<SC_SIZE || cmd>SC_RESTORE))
2262 MessageBoxW(hwnd, RS(b2,IDS_NO_IMPL), RS(b1,IDS_WINEFILE), MB_OK);
2264 return DefFrameProcW(hwnd, Globals.hmdiclient, nmsg, wparam, lparam);
2266 break;}
2268 case WM_SIZE:
2269 resize_frame(hwnd, LOWORD(lparam), HIWORD(lparam));
2270 break; /* do not pass message to DefFrameProcW */
2272 case WM_DEVICECHANGE:
2273 SendMessageW(hwnd, WM_COMMAND, MAKELONG(ID_REFRESH,0), 0);
2274 break;
2276 case WM_GETMINMAXINFO: {
2277 LPMINMAXINFO lpmmi = (LPMINMAXINFO)lparam;
2279 lpmmi->ptMaxTrackSize.x <<= 1;/*2*GetSystemMetrics(SM_CXSCREEN) / SM_CXVIRTUALSCREEN */
2280 lpmmi->ptMaxTrackSize.y <<= 1;/*2*GetSystemMetrics(SM_CYSCREEN) / SM_CYVIRTUALSCREEN */
2281 break;}
2283 case FRM_CALC_CLIENT:
2284 frame_get_clientspace(hwnd, (PRECT)lparam);
2285 return TRUE;
2287 default:
2288 return DefFrameProcW(hwnd, Globals.hmdiclient, nmsg, wparam, lparam);
2291 return 0;
2295 static WCHAR g_pos_names[COLUMNS][40] = {
2296 {'\0'} /* symbol */
2299 static const int g_pos_align[] = {
2301 HDF_LEFT, /* Name */
2302 HDF_RIGHT, /* Size */
2303 HDF_LEFT, /* CDate */
2304 HDF_LEFT, /* ADate */
2305 HDF_LEFT, /* MDate */
2306 HDF_LEFT, /* Index */
2307 HDF_CENTER, /* Links */
2308 HDF_CENTER, /* Attributes */
2309 HDF_LEFT /* Security */
2312 static void resize_tree(ChildWnd* child, int cx, int cy)
2314 HDWP hdwp = BeginDeferWindowPos(4);
2315 RECT rt;
2316 WINDOWPOS wp;
2317 HD_LAYOUT hdl;
2319 rt.left = 0;
2320 rt.top = 0;
2321 rt.right = cx;
2322 rt.bottom = cy;
2324 cx = child->split_pos + SPLIT_WIDTH/2;
2325 hdl.prc = &rt;
2326 hdl.pwpos = &wp;
2328 SendMessageW(child->left.hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hdl);
2330 DeferWindowPos(hdwp, child->left.hwndHeader, wp.hwndInsertAfter,
2331 wp.x-1, wp.y, child->split_pos-SPLIT_WIDTH/2+1, wp.cy, wp.flags);
2332 DeferWindowPos(hdwp, child->right.hwndHeader, wp.hwndInsertAfter,
2333 rt.left+cx+1, wp.y, wp.cx-cx+2, wp.cy, wp.flags);
2334 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);
2335 DeferWindowPos(hdwp, child->right.hwnd, 0, rt.left+cx+1, rt.top, rt.right-cx, rt.bottom-rt.top, SWP_NOZORDER|SWP_NOACTIVATE);
2337 EndDeferWindowPos(hdwp);
2340 static HWND create_header(HWND parent, Pane* pane, UINT id)
2342 HDITEMW hdi;
2343 int idx;
2345 HWND hwnd = CreateWindowW(WC_HEADERW, 0, WS_CHILD|WS_VISIBLE|HDS_HORZ|HDS_FULLDRAG/*TODO: |HDS_BUTTONS + sort orders*/,
2346 0, 0, 0, 0, parent, (HMENU)ULongToHandle(id), Globals.hInstance, 0);
2347 if (!hwnd)
2348 return 0;
2350 SendMessageW(hwnd, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), FALSE);
2352 hdi.mask = HDI_TEXT|HDI_WIDTH|HDI_FORMAT;
2354 for(idx=0; idx<COLUMNS; idx++) {
2355 hdi.pszText = g_pos_names[idx];
2356 hdi.fmt = HDF_STRING | g_pos_align[idx];
2357 hdi.cxy = pane->widths[idx];
2358 SendMessageW(hwnd, HDM_INSERTITEMW, idx, (LPARAM)&hdi);
2361 return hwnd;
2364 static void init_output(HWND hwnd)
2366 static const WCHAR s1000[] = {'1','0','0','0','\0'};
2367 WCHAR b[16];
2368 HFONT old_font;
2369 HDC hdc = GetDC(hwnd);
2371 if (GetNumberFormatW(LOCALE_USER_DEFAULT, 0, s1000, 0, b, 16) > 4)
2372 Globals.num_sep = b[1];
2373 else
2374 Globals.num_sep = '.';
2376 old_font = SelectObject(hdc, Globals.hfont);
2377 GetTextExtentPoint32W(hdc, sSpace, 1, &Globals.spaceSize);
2378 SelectObject(hdc, old_font);
2379 ReleaseDC(hwnd, hdc);
2382 static void draw_item(Pane* pane, LPDRAWITEMSTRUCT dis, Entry* entry, int calcWidthCol);
2385 /* calculate preferred width for all visible columns */
2387 static BOOL calc_widths(Pane* pane, BOOL anyway)
2389 int col, x, cx, spc=3*Globals.spaceSize.cx;
2390 int entries = SendMessageW(pane->hwnd, LB_GETCOUNT, 0, 0);
2391 int orgWidths[COLUMNS];
2392 int orgPositions[COLUMNS+1];
2393 HFONT hfontOld;
2394 HDC hdc;
2395 int cnt;
2397 if (!anyway) {
2398 memcpy(orgWidths, pane->widths, sizeof(orgWidths));
2399 memcpy(orgPositions, pane->positions, sizeof(orgPositions));
2402 for(col=0; col<COLUMNS; col++)
2403 pane->widths[col] = 0;
2405 hdc = GetDC(pane->hwnd);
2406 hfontOld = SelectObject(hdc, Globals.hfont);
2408 for(cnt=0; cnt<entries; cnt++) {
2409 Entry* entry = (Entry*)SendMessageW(pane->hwnd, LB_GETITEMDATA, cnt, 0);
2411 DRAWITEMSTRUCT dis;
2413 dis.CtlType = 0;
2414 dis.CtlID = 0;
2415 dis.itemID = 0;
2416 dis.itemAction = 0;
2417 dis.itemState = 0;
2418 dis.hwndItem = pane->hwnd;
2419 dis.hDC = hdc;
2420 dis.rcItem.left = 0;
2421 dis.rcItem.top = 0;
2422 dis.rcItem.right = 0;
2423 dis.rcItem.bottom = 0;
2424 /*dis.itemData = 0; */
2426 draw_item(pane, &dis, entry, COLUMNS);
2429 SelectObject(hdc, hfontOld);
2430 ReleaseDC(pane->hwnd, hdc);
2432 x = 0;
2433 for(col=0; col<COLUMNS; col++) {
2434 pane->positions[col] = x;
2435 cx = pane->widths[col];
2437 if (cx) {
2438 cx += spc;
2440 if (cx < IMAGE_WIDTH)
2441 cx = IMAGE_WIDTH;
2443 pane->widths[col] = cx;
2446 x += cx;
2449 pane->positions[COLUMNS] = x;
2451 SendMessageW(pane->hwnd, LB_SETHORIZONTALEXTENT, x, 0);
2453 /* no change? */
2454 if (!anyway && !memcmp(orgWidths, pane->widths, sizeof(orgWidths)))
2455 return FALSE;
2457 /* don't move, if only collapsing an entry */
2458 if (!anyway && pane->widths[0]<orgWidths[0] &&
2459 !memcmp(orgWidths+1, pane->widths+1, sizeof(orgWidths)-sizeof(int))) {
2460 pane->widths[0] = orgWidths[0];
2461 memcpy(pane->positions, orgPositions, sizeof(orgPositions));
2463 return FALSE;
2466 InvalidateRect(pane->hwnd, 0, TRUE);
2468 return TRUE;
2471 /* calculate one preferred column width */
2472 static void calc_single_width(Pane* pane, int col)
2474 HFONT hfontOld;
2475 int x, cx;
2476 int entries = SendMessageW(pane->hwnd, LB_GETCOUNT, 0, 0);
2477 int cnt;
2478 HDC hdc;
2480 pane->widths[col] = 0;
2482 hdc = GetDC(pane->hwnd);
2483 hfontOld = SelectObject(hdc, Globals.hfont);
2485 for(cnt=0; cnt<entries; cnt++) {
2486 Entry* entry = (Entry*)SendMessageW(pane->hwnd, LB_GETITEMDATA, cnt, 0);
2487 DRAWITEMSTRUCT dis;
2489 dis.CtlType = 0;
2490 dis.CtlID = 0;
2491 dis.itemID = 0;
2492 dis.itemAction = 0;
2493 dis.itemState = 0;
2494 dis.hwndItem = pane->hwnd;
2495 dis.hDC = hdc;
2496 dis.rcItem.left = 0;
2497 dis.rcItem.top = 0;
2498 dis.rcItem.right = 0;
2499 dis.rcItem.bottom = 0;
2500 /*dis.itemData = 0; */
2502 draw_item(pane, &dis, entry, col);
2505 SelectObject(hdc, hfontOld);
2506 ReleaseDC(pane->hwnd, hdc);
2508 cx = pane->widths[col];
2510 if (cx) {
2511 cx += 3*Globals.spaceSize.cx;
2513 if (cx < IMAGE_WIDTH)
2514 cx = IMAGE_WIDTH;
2517 pane->widths[col] = cx;
2519 x = pane->positions[col] + cx;
2521 for(; col<COLUMNS-1; ) {
2522 pane->positions[++col] = x;
2523 x += pane->widths[col];
2526 SendMessageW(pane->hwnd, LB_SETHORIZONTALEXTENT, x, 0);
2529 static BOOL pattern_match(LPCWSTR str, LPCWSTR pattern)
2531 for( ; *str&&*pattern; str++,pattern++) {
2532 if (*pattern == '*') {
2533 do pattern++;
2534 while(*pattern == '*');
2536 if (!*pattern)
2537 return TRUE;
2539 for(; *str; str++)
2540 if (*str==*pattern && pattern_match(str, pattern))
2541 return TRUE;
2543 return FALSE;
2545 else if (*str!=*pattern && *pattern!='?')
2546 return FALSE;
2549 if (*str || *pattern)
2550 if (*pattern!='*' || pattern[1]!='\0')
2551 return FALSE;
2553 return TRUE;
2556 static BOOL pattern_imatch(LPCWSTR str, LPCWSTR pattern)
2558 WCHAR b1[BUFFER_LEN], b2[BUFFER_LEN];
2560 lstrcpyW(b1, str);
2561 lstrcpyW(b2, pattern);
2562 CharUpperW(b1);
2563 CharUpperW(b2);
2565 return pattern_match(b1, b2);
2569 enum FILE_TYPE {
2570 FT_OTHER = 0,
2571 FT_EXECUTABLE = 1,
2572 FT_DOCUMENT = 2
2575 static enum FILE_TYPE get_file_type(LPCWSTR filename);
2578 /* insert listbox entries after index idx */
2580 static int insert_entries(Pane* pane, Entry* dir, LPCWSTR pattern, int filter_flags, int idx)
2582 Entry* entry = dir;
2584 if (!entry)
2585 return idx;
2587 ShowWindow(pane->hwnd, SW_HIDE);
2589 for(; entry; entry=entry->next) {
2590 if (pane->treePane && !(entry->data.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY))
2591 continue;
2593 if (entry->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
2594 /* don't display entries "." and ".." in the left pane */
2595 if (pane->treePane && entry->data.cFileName[0] == '.')
2596 if (entry->data.cFileName[1] == '\0' ||
2597 (entry->data.cFileName[1] == '.' &&
2598 entry->data.cFileName[2] == '\0'))
2599 continue;
2601 /* filter directories in right pane */
2602 if (!pane->treePane && !(filter_flags&TF_DIRECTORIES))
2603 continue;
2606 /* filter using the file name pattern */
2607 if (pattern)
2608 if (!pattern_imatch(entry->data.cFileName, pattern))
2609 continue;
2611 /* filter system and hidden files */
2612 if (!(filter_flags&TF_HIDDEN) && (entry->data.dwFileAttributes&(FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM)))
2613 continue;
2615 /* filter looking at the file type */
2616 if ((filter_flags&(TF_PROGRAMS|TF_DOCUMENTS|TF_OTHERS)) != (TF_PROGRAMS|TF_DOCUMENTS|TF_OTHERS))
2617 switch(get_file_type(entry->data.cFileName)) {
2618 case FT_EXECUTABLE:
2619 if (!(filter_flags & TF_PROGRAMS))
2620 continue;
2621 break;
2623 case FT_DOCUMENT:
2624 if (!(filter_flags & TF_DOCUMENTS))
2625 continue;
2626 break;
2628 default: /* TF_OTHERS */
2629 if (!(filter_flags & TF_OTHERS))
2630 continue;
2633 if (idx != -1)
2634 idx++;
2636 SendMessageW(pane->hwnd, LB_INSERTSTRING, idx, (LPARAM)entry);
2638 if (pane->treePane && entry->expanded)
2639 idx = insert_entries(pane, entry->down, pattern, filter_flags, idx);
2642 ShowWindow(pane->hwnd, SW_SHOW);
2644 return idx;
2648 static void format_bytes(LPWSTR buffer, LONGLONG bytes)
2650 static const WCHAR sFmtSmall[] = {'%', 'u', 0};
2651 static const WCHAR sFmtBig[] = {'%', '.', '1', 'f', ' ', '%', 's', '\0'};
2653 if (bytes < 1024)
2654 sprintfW(buffer, sFmtSmall, (DWORD)bytes);
2655 else
2657 WCHAR unit[64];
2658 UINT resid;
2659 float fBytes;
2660 if (bytes >= 1073741824) /* 1 GB */
2662 fBytes = ((float)bytes)/1073741824.f+.5f;
2663 resid = IDS_UNIT_GB;
2665 else if (bytes >= 1048576) /* 1 MB */
2667 fBytes = ((float)bytes)/1048576.f+.5f;
2668 resid = IDS_UNIT_MB;
2670 else /* bytes >= 1024 */ /* 1 kB */
2672 fBytes = ((float)bytes)/1024.f+.5f;
2673 resid = IDS_UNIT_KB;
2675 LoadStringW(Globals.hInstance, resid, unit, sizeof(unit)/sizeof(*unit));
2676 sprintfW(buffer, sFmtBig, fBytes, unit);
2680 static void set_space_status(void)
2682 ULARGE_INTEGER ulFreeBytesToCaller, ulTotalBytes, ulFreeBytes;
2683 WCHAR fmt[64], b1[64], b2[64], buffer[BUFFER_LEN];
2685 if (GetDiskFreeSpaceExW(NULL, &ulFreeBytesToCaller, &ulTotalBytes, &ulFreeBytes)) {
2686 DWORD_PTR args[2];
2687 format_bytes(b1, ulFreeBytesToCaller.QuadPart);
2688 format_bytes(b2, ulTotalBytes.QuadPart);
2689 args[0] = (DWORD_PTR)b1;
2690 args[1] = (DWORD_PTR)b2;
2691 FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
2692 RS(fmt,IDS_FREE_SPACE_FMT), 0, 0, buffer,
2693 sizeof(buffer)/sizeof(*buffer), (__ms_va_list*)args);
2694 } else
2695 lstrcpyW(buffer, sQMarks);
2697 SendMessageW(Globals.hstatusbar, SB_SETTEXTW, 0, (LPARAM)buffer);
2701 static WNDPROC g_orgTreeWndProc;
2703 static void create_tree_window(HWND parent, Pane* pane, UINT id, UINT id_header, LPCWSTR pattern, int filter_flags)
2705 static const WCHAR sListBox[] = {'L','i','s','t','B','o','x','\0'};
2707 static BOOL s_init = FALSE;
2708 Entry* entry = pane->root;
2710 pane->hwnd = CreateWindowW(sListBox, sEmpty, WS_CHILD|WS_VISIBLE|WS_HSCROLL|WS_VSCROLL|
2711 LBS_DISABLENOSCROLL|LBS_NOINTEGRALHEIGHT|LBS_OWNERDRAWFIXED|LBS_NOTIFY,
2712 0, 0, 0, 0, parent, (HMENU)ULongToHandle(id), Globals.hInstance, 0);
2714 SetWindowLongPtrW(pane->hwnd, GWLP_USERDATA, (LPARAM)pane);
2715 g_orgTreeWndProc = (WNDPROC)SetWindowLongPtrW(pane->hwnd, GWLP_WNDPROC, (LPARAM)TreeWndProc);
2717 SendMessageW(pane->hwnd, WM_SETFONT, (WPARAM)Globals.hfont, FALSE);
2719 /* insert entries into listbox */
2720 if (entry)
2721 insert_entries(pane, entry, pattern, filter_flags, -1);
2723 /* calculate column widths */
2724 if (!s_init) {
2725 s_init = TRUE;
2726 init_output(pane->hwnd);
2729 calc_widths(pane, TRUE);
2731 pane->hwndHeader = create_header(parent, pane, id_header);
2735 static void InitChildWindow(ChildWnd* child)
2737 create_tree_window(child->hwnd, &child->left, IDW_TREE_LEFT, IDW_HEADER_LEFT, NULL, TF_ALL);
2738 create_tree_window(child->hwnd, &child->right, IDW_TREE_RIGHT, IDW_HEADER_RIGHT, child->filter_pattern, child->filter_flags);
2742 static void format_date(const FILETIME* ft, WCHAR* buffer, int visible_cols)
2744 SYSTEMTIME systime;
2745 FILETIME lft;
2746 int len = 0;
2748 *buffer = '\0';
2750 if (!ft->dwLowDateTime && !ft->dwHighDateTime)
2751 return;
2753 if (!FileTimeToLocalFileTime(ft, &lft))
2754 {err: lstrcpyW(buffer,sQMarks); return;}
2756 if (!FileTimeToSystemTime(&lft, &systime))
2757 goto err;
2759 if (visible_cols & COL_DATE) {
2760 len = GetDateFormatW(LOCALE_USER_DEFAULT, 0, &systime, 0, buffer, BUFFER_LEN);
2761 if (!len)
2762 goto err;
2765 if (visible_cols & COL_TIME) {
2766 if (len)
2767 buffer[len-1] = ' ';
2769 buffer[len++] = ' ';
2771 if (!GetTimeFormatW(LOCALE_USER_DEFAULT, 0, &systime, 0, buffer+len, BUFFER_LEN-len))
2772 buffer[len] = '\0';
2777 static void calc_width(Pane* pane, LPDRAWITEMSTRUCT dis, int col, LPCWSTR str)
2779 RECT rt = {0, 0, 0, 0};
2781 DrawTextW(dis->hDC, str, -1, &rt, DT_CALCRECT|DT_SINGLELINE|DT_NOPREFIX);
2783 if (rt.right > pane->widths[col])
2784 pane->widths[col] = rt.right;
2787 static void calc_tabbed_width(Pane* pane, LPDRAWITEMSTRUCT dis, int col, LPCWSTR str)
2789 RECT rt = {0, 0, 0, 0};
2791 DrawTextW(dis->hDC, str, -1, &rt, DT_CALCRECT|DT_SINGLELINE|DT_EXPANDTABS|DT_TABSTOP|(2<<8));
2792 /*FIXME rt (0,0) ??? */
2794 if (rt.right > pane->widths[col])
2795 pane->widths[col] = rt.right;
2799 static void output_text(Pane* pane, LPDRAWITEMSTRUCT dis, int col, LPCWSTR str, DWORD flags)
2801 int x = dis->rcItem.left;
2802 RECT rt;
2804 rt.left = x+pane->positions[col]+Globals.spaceSize.cx;
2805 rt.top = dis->rcItem.top;
2806 rt.right = x+pane->positions[col+1]-Globals.spaceSize.cx;
2807 rt.bottom = dis->rcItem.bottom;
2809 DrawTextW(dis->hDC, str, -1, &rt, DT_SINGLELINE|DT_NOPREFIX|flags);
2812 static void output_tabbed_text(Pane* pane, LPDRAWITEMSTRUCT dis, int col, LPCWSTR str)
2814 int x = dis->rcItem.left;
2815 RECT rt;
2817 rt.left = x+pane->positions[col]+Globals.spaceSize.cx;
2818 rt.top = dis->rcItem.top;
2819 rt.right = x+pane->positions[col+1]-Globals.spaceSize.cx;
2820 rt.bottom = dis->rcItem.bottom;
2822 DrawTextW(dis->hDC, str, -1, &rt, DT_SINGLELINE|DT_EXPANDTABS|DT_TABSTOP|(2<<8));
2825 static void output_number(Pane* pane, LPDRAWITEMSTRUCT dis, int col, LPCWSTR str)
2827 int x = dis->rcItem.left;
2828 RECT rt;
2829 LPCWSTR s = str;
2830 WCHAR b[128];
2831 LPWSTR d = b;
2832 int pos;
2834 rt.left = x+pane->positions[col]+Globals.spaceSize.cx;
2835 rt.top = dis->rcItem.top;
2836 rt.right = x+pane->positions[col+1]-Globals.spaceSize.cx;
2837 rt.bottom = dis->rcItem.bottom;
2839 if (*s)
2840 *d++ = *s++;
2842 /* insert number separator characters */
2843 pos = lstrlenW(s) % 3;
2845 while(*s)
2846 if (pos--)
2847 *d++ = *s++;
2848 else {
2849 *d++ = Globals.num_sep;
2850 pos = 3;
2853 DrawTextW(dis->hDC, b, d-b, &rt, DT_RIGHT|DT_SINGLELINE|DT_NOPREFIX|DT_END_ELLIPSIS);
2857 static BOOL is_exe_file(LPCWSTR ext)
2859 static const WCHAR executable_extensions[][4] = {
2860 {'C','O','M','\0'},
2861 {'E','X','E','\0'},
2862 {'B','A','T','\0'},
2863 {'C','M','D','\0'},
2864 {'C','M','M','\0'},
2865 {'B','T','M','\0'},
2866 {'A','W','K','\0'},
2867 {'\0'}
2870 WCHAR ext_buffer[_MAX_EXT];
2871 const WCHAR (*p)[4];
2872 LPCWSTR s;
2873 LPWSTR d;
2875 for(s=ext+1,d=ext_buffer; (*d=tolower(*s)); s++)
2876 d++;
2878 for(p=executable_extensions; (*p)[0]; p++)
2879 if (!lstrcmpiW(ext_buffer, *p))
2880 return TRUE;
2882 return FALSE;
2885 static BOOL is_registered_type(LPCWSTR ext)
2887 /* check if there exists a classname for this file extension in the registry */
2888 if (!RegQueryValueW(HKEY_CLASSES_ROOT, ext, NULL, NULL))
2889 return TRUE;
2891 return FALSE;
2894 static enum FILE_TYPE get_file_type(LPCWSTR filename)
2896 LPCWSTR ext = strrchrW(filename, '.');
2897 if (!ext)
2898 ext = sEmpty;
2900 if (is_exe_file(ext))
2901 return FT_EXECUTABLE;
2902 else if (is_registered_type(ext))
2903 return FT_DOCUMENT;
2904 else
2905 return FT_OTHER;
2909 static void draw_item(Pane* pane, LPDRAWITEMSTRUCT dis, Entry* entry, int calcWidthCol)
2911 WCHAR buffer[BUFFER_LEN];
2912 DWORD attrs;
2913 int visible_cols = pane->visible_cols;
2914 COLORREF bkcolor, textcolor;
2915 RECT focusRect = dis->rcItem;
2916 HBRUSH hbrush;
2917 enum IMAGE img;
2918 int img_pos, cx;
2919 int col = 0;
2921 if (entry) {
2922 attrs = entry->data.dwFileAttributes;
2924 if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
2925 if (entry->data.cFileName[0] == '.' && entry->data.cFileName[1] == '.'
2926 && entry->data.cFileName[2] == '\0')
2927 img = IMG_FOLDER_UP;
2928 else if (entry->data.cFileName[0] == '.' && entry->data.cFileName[1] == '\0')
2929 img = IMG_FOLDER_CUR;
2930 else if (pane->treePane && (dis->itemState&ODS_FOCUS))
2931 img = IMG_OPEN_FOLDER;
2932 else
2933 img = IMG_FOLDER;
2934 } else {
2935 switch(get_file_type(entry->data.cFileName)) {
2936 case FT_EXECUTABLE: img = IMG_EXECUTABLE; break;
2937 case FT_DOCUMENT: img = IMG_DOCUMENT; break;
2938 default: img = IMG_FILE;
2941 } else {
2942 attrs = 0;
2943 img = IMG_NONE;
2946 if (pane->treePane) {
2947 if (entry) {
2948 img_pos = dis->rcItem.left + entry->level*(IMAGE_WIDTH+TREE_LINE_DX);
2950 if (calcWidthCol == -1) {
2951 int x;
2952 int y = dis->rcItem.top + IMAGE_HEIGHT/2;
2953 Entry* up;
2954 RECT rt_clip;
2955 HRGN hrgn_org = CreateRectRgn(0, 0, 0, 0);
2956 HRGN hrgn;
2958 rt_clip.left = dis->rcItem.left;
2959 rt_clip.top = dis->rcItem.top;
2960 rt_clip.right = dis->rcItem.left+pane->widths[col];
2961 rt_clip.bottom = dis->rcItem.bottom;
2963 hrgn = CreateRectRgnIndirect(&rt_clip);
2965 if (!GetClipRgn(dis->hDC, hrgn_org)) {
2966 DeleteObject(hrgn_org);
2967 hrgn_org = 0;
2970 ExtSelectClipRgn(dis->hDC, hrgn, RGN_AND);
2971 DeleteObject(hrgn);
2973 if ((up=entry->up) != NULL) {
2974 MoveToEx(dis->hDC, img_pos-IMAGE_WIDTH/2, y, 0);
2975 LineTo(dis->hDC, img_pos-2, y);
2977 x = img_pos - IMAGE_WIDTH/2;
2979 do {
2980 x -= IMAGE_WIDTH+TREE_LINE_DX;
2982 if (up->next
2983 && (up->next->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
2985 MoveToEx(dis->hDC, x, dis->rcItem.top, 0);
2986 LineTo(dis->hDC, x, dis->rcItem.bottom);
2988 } while((up=up->up) != NULL);
2991 x = img_pos - IMAGE_WIDTH/2;
2993 MoveToEx(dis->hDC, x, dis->rcItem.top, 0);
2994 LineTo(dis->hDC, x, y);
2996 if (entry->next
2997 && (entry->next->data.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY))
2998 LineTo(dis->hDC, x, dis->rcItem.bottom);
3000 SelectClipRgn(dis->hDC, hrgn_org);
3001 if (hrgn_org) DeleteObject(hrgn_org);
3002 } else if (calcWidthCol==col || calcWidthCol==COLUMNS) {
3003 int right = img_pos + IMAGE_WIDTH - TREE_LINE_DX;
3005 if (right > pane->widths[col])
3006 pane->widths[col] = right;
3008 } else {
3009 img_pos = dis->rcItem.left;
3011 } else {
3012 img_pos = dis->rcItem.left;
3014 if (calcWidthCol==col || calcWidthCol==COLUMNS)
3015 pane->widths[col] = IMAGE_WIDTH;
3018 if (calcWidthCol == -1) {
3019 focusRect.left = img_pos -2;
3021 if (attrs & FILE_ATTRIBUTE_COMPRESSED)
3022 textcolor = COLOR_COMPRESSED;
3023 else
3024 textcolor = RGB(0,0,0);
3026 if (dis->itemState & ODS_FOCUS) {
3027 textcolor = RGB(255,255,255);
3028 bkcolor = COLOR_SELECTION;
3029 } else {
3030 bkcolor = RGB(255,255,255);
3033 hbrush = CreateSolidBrush(bkcolor);
3034 FillRect(dis->hDC, &focusRect, hbrush);
3035 DeleteObject(hbrush);
3037 SetBkMode(dis->hDC, TRANSPARENT);
3038 SetTextColor(dis->hDC, textcolor);
3040 cx = pane->widths[col];
3042 if (cx && img!=IMG_NONE) {
3043 if (cx > IMAGE_WIDTH)
3044 cx = IMAGE_WIDTH;
3046 if (entry->hicon && entry->hicon!=(HICON)-1)
3047 DrawIconEx(dis->hDC, img_pos, dis->rcItem.top, entry->hicon, cx, GetSystemMetrics(SM_CYSMICON), 0, 0, DI_NORMAL);
3048 else
3049 ImageList_DrawEx(Globals.himl, img, dis->hDC,
3050 img_pos, dis->rcItem.top, cx,
3051 IMAGE_HEIGHT, bkcolor, CLR_DEFAULT, ILD_NORMAL);
3055 if (!entry)
3056 return;
3058 col++;
3060 /* output file name */
3061 if (calcWidthCol == -1)
3062 output_text(pane, dis, col, entry->data.cFileName, 0);
3063 else if (calcWidthCol==col || calcWidthCol==COLUMNS)
3064 calc_width(pane, dis, col, entry->data.cFileName);
3066 col++;
3068 /* display file size */
3069 if (visible_cols & COL_SIZE) {
3070 format_longlong( buffer, ((ULONGLONG)entry->data.nFileSizeHigh << 32) | entry->data.nFileSizeLow );
3072 if (calcWidthCol == -1)
3073 output_number(pane, dis, col, buffer);
3074 else if (calcWidthCol==col || calcWidthCol==COLUMNS)
3075 calc_width(pane, dis, col, buffer);/*TODO: not ever time enough */
3077 col++;
3080 /* display file date */
3081 if (visible_cols & (COL_DATE|COL_TIME)) {
3082 format_date(&entry->data.ftCreationTime, buffer, visible_cols);
3083 if (calcWidthCol == -1)
3084 output_text(pane, dis, col, buffer, 0);
3085 else if (calcWidthCol==col || calcWidthCol==COLUMNS)
3086 calc_width(pane, dis, col, buffer);
3087 col++;
3089 format_date(&entry->data.ftLastAccessTime, buffer, visible_cols);
3090 if (calcWidthCol == -1)
3091 output_text(pane, dis, col, buffer, 0);
3092 else if (calcWidthCol==col || calcWidthCol==COLUMNS)
3093 calc_width(pane, dis, col, buffer);
3094 col++;
3096 format_date(&entry->data.ftLastWriteTime, buffer, visible_cols);
3097 if (calcWidthCol == -1)
3098 output_text(pane, dis, col, buffer, 0);
3099 else if (calcWidthCol==col || calcWidthCol==COLUMNS)
3100 calc_width(pane, dis, col, buffer);
3101 col++;
3104 if (entry->bhfi_valid) {
3105 if (visible_cols & COL_INDEX) {
3106 static const WCHAR fmtlow[] = {'%','X',0};
3107 static const WCHAR fmthigh[] = {'%','X','%','0','8','X',0};
3109 if (entry->bhfi.nFileIndexHigh)
3110 wsprintfW(buffer, fmthigh,
3111 entry->bhfi.nFileIndexHigh, entry->bhfi.nFileIndexLow );
3112 else
3113 wsprintfW(buffer, fmtlow, entry->bhfi.nFileIndexLow );
3115 if (calcWidthCol == -1)
3116 output_text(pane, dis, col, buffer, DT_RIGHT);
3117 else if (calcWidthCol==col || calcWidthCol==COLUMNS)
3118 calc_width(pane, dis, col, buffer);
3120 col++;
3123 if (visible_cols & COL_LINKS) {
3124 wsprintfW(buffer, sNumFmt, entry->bhfi.nNumberOfLinks);
3126 if (calcWidthCol == -1)
3127 output_text(pane, dis, col, buffer, DT_CENTER);
3128 else if (calcWidthCol==col || calcWidthCol==COLUMNS)
3129 calc_width(pane, dis, col, buffer);
3131 col++;
3133 } else
3134 col += 2;
3136 /* show file attributes */
3137 if (visible_cols & COL_ATTRIBUTES) {
3138 static const WCHAR s11Tabs[] = {' ','\t',' ','\t',' ','\t',' ','\t',' ','\t',' ','\t',' ','\t',' ','\t',' ','\t',' ','\t',' ','\t',' ','\0'};
3139 lstrcpyW(buffer, s11Tabs);
3141 if (attrs & FILE_ATTRIBUTE_NORMAL) buffer[ 0] = 'N';
3142 else {
3143 if (attrs & FILE_ATTRIBUTE_READONLY) buffer[ 2] = 'R';
3144 if (attrs & FILE_ATTRIBUTE_HIDDEN) buffer[ 4] = 'H';
3145 if (attrs & FILE_ATTRIBUTE_SYSTEM) buffer[ 6] = 'S';
3146 if (attrs & FILE_ATTRIBUTE_ARCHIVE) buffer[ 8] = 'A';
3147 if (attrs & FILE_ATTRIBUTE_COMPRESSED) buffer[10] = 'C';
3148 if (attrs & FILE_ATTRIBUTE_DIRECTORY) buffer[12] = 'D';
3149 if (attrs & FILE_ATTRIBUTE_ENCRYPTED) buffer[14] = 'E';
3150 if (attrs & FILE_ATTRIBUTE_TEMPORARY) buffer[16] = 'T';
3151 if (attrs & FILE_ATTRIBUTE_SPARSE_FILE) buffer[18] = 'P';
3152 if (attrs & FILE_ATTRIBUTE_REPARSE_POINT) buffer[20] = 'Q';
3153 if (attrs & FILE_ATTRIBUTE_OFFLINE) buffer[22] = 'O';
3154 if (attrs & FILE_ATTRIBUTE_NOT_CONTENT_INDEXED) buffer[24] = 'X';
3157 if (calcWidthCol == -1)
3158 output_tabbed_text(pane, dis, col, buffer);
3159 else if (calcWidthCol==col || calcWidthCol==COLUMNS)
3160 calc_tabbed_width(pane, dis, col, buffer);
3162 col++;
3166 static void set_header(Pane* pane)
3168 HDITEMW item;
3169 int scroll_pos = GetScrollPos(pane->hwnd, SB_HORZ);
3170 int i=0, x=0;
3172 item.mask = HDI_WIDTH;
3173 item.cxy = 0;
3175 for(; (i < COLUMNS) && (x+pane->widths[i] < scroll_pos); i++) {
3176 x += pane->widths[i];
3177 SendMessageW(pane->hwndHeader, HDM_SETITEMW, i, (LPARAM)&item);
3180 if (i < COLUMNS) {
3181 x += pane->widths[i];
3182 item.cxy = x - scroll_pos;
3183 SendMessageW(pane->hwndHeader, HDM_SETITEMW, i++, (LPARAM)&item);
3185 for(; i < COLUMNS; i++) {
3186 item.cxy = pane->widths[i];
3187 x += pane->widths[i];
3188 SendMessageW(pane->hwndHeader, HDM_SETITEMW, i, (LPARAM)&item);
3193 static LRESULT pane_notify(Pane* pane, NMHDR* pnmh)
3195 switch(pnmh->code) {
3196 case HDN_ITEMCHANGEDW: {
3197 LPNMHEADERW phdn = (LPNMHEADERW)pnmh;
3198 int idx = phdn->iItem;
3199 int dx = phdn->pitem->cxy - pane->widths[idx];
3200 int i;
3202 RECT clnt;
3203 GetClientRect(pane->hwnd, &clnt);
3205 pane->widths[idx] += dx;
3207 for(i=idx; ++i<=COLUMNS; )
3208 pane->positions[i] += dx;
3211 int scroll_pos = GetScrollPos(pane->hwnd, SB_HORZ);
3212 RECT rt_scr;
3213 RECT rt_clip;
3215 rt_scr.left = pane->positions[idx+1]-scroll_pos;
3216 rt_scr.top = 0;
3217 rt_scr.right = clnt.right;
3218 rt_scr.bottom = clnt.bottom;
3220 rt_clip.left = pane->positions[idx]-scroll_pos;
3221 rt_clip.top = 0;
3222 rt_clip.right = clnt.right;
3223 rt_clip.bottom = clnt.bottom;
3225 if (rt_scr.left < 0) rt_scr.left = 0;
3226 if (rt_clip.left < 0) rt_clip.left = 0;
3228 ScrollWindowEx(pane->hwnd, dx, 0, &rt_scr, &rt_clip, 0, 0, SW_INVALIDATE);
3230 rt_clip.right = pane->positions[idx+1];
3231 RedrawWindow(pane->hwnd, &rt_clip, 0, RDW_INVALIDATE|RDW_UPDATENOW);
3233 if (pnmh->code == HDN_ENDTRACKW) {
3234 SendMessageW(pane->hwnd, LB_SETHORIZONTALEXTENT, pane->positions[COLUMNS], 0);
3236 if (GetScrollPos(pane->hwnd, SB_HORZ) != scroll_pos)
3237 set_header(pane);
3241 return FALSE;
3244 case HDN_DIVIDERDBLCLICKW: {
3245 LPNMHEADERW phdn = (LPNMHEADERW)pnmh;
3246 HDITEMW item;
3248 calc_single_width(pane, phdn->iItem);
3249 item.mask = HDI_WIDTH;
3250 item.cxy = pane->widths[phdn->iItem];
3252 SendMessageW(pane->hwndHeader, HDM_SETITEMW, phdn->iItem, (LPARAM)&item);
3253 InvalidateRect(pane->hwnd, 0, TRUE);
3254 break;}
3257 return 0;
3260 static void scan_entry(ChildWnd* child, Entry* entry, int idx, HWND hwnd)
3262 WCHAR path[MAX_PATH];
3263 HCURSOR old_cursor = SetCursor(LoadCursorW(0, (LPCWSTR)IDC_WAIT));
3265 /* delete sub entries in left pane */
3266 for(;;) {
3267 LRESULT res = SendMessageW(child->left.hwnd, LB_GETITEMDATA, idx+1, 0);
3268 Entry* sub = (Entry*) res;
3270 if (res==LB_ERR || !sub || sub->level<=entry->level)
3271 break;
3273 SendMessageW(child->left.hwnd, LB_DELETESTRING, idx+1, 0);
3276 /* empty right pane */
3277 SendMessageW(child->right.hwnd, LB_RESETCONTENT, 0, 0);
3279 /* release memory */
3280 free_entries(entry);
3282 /* read contents from disk */
3283 if (entry->etype == ET_SHELL)
3285 read_directory(entry, NULL, child->sortOrder, hwnd);
3287 else
3289 get_path(entry, path);
3290 read_directory(entry, path, child->sortOrder, hwnd);
3293 /* insert found entries in right pane */
3294 insert_entries(&child->right, entry->down, child->filter_pattern, child->filter_flags, -1);
3295 calc_widths(&child->right, FALSE);
3296 set_header(&child->right);
3298 child->header_wdths_ok = FALSE;
3300 SetCursor(old_cursor);
3304 /* expand a directory entry */
3306 static BOOL expand_entry(ChildWnd* child, Entry* dir)
3308 int idx;
3309 Entry* p;
3311 if (!dir || dir->expanded || !dir->down)
3312 return FALSE;
3314 p = dir->down;
3316 if (p->data.cFileName[0]=='.' && p->data.cFileName[1]=='\0' && p->next) {
3317 p = p->next;
3319 if (p->data.cFileName[0]=='.' && p->data.cFileName[1]=='.' &&
3320 p->data.cFileName[2]=='\0' && p->next)
3321 p = p->next;
3324 /* no subdirectories ? */
3325 if (!(p->data.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY))
3326 return FALSE;
3328 idx = SendMessageW(child->left.hwnd, LB_FINDSTRING, 0, (LPARAM)dir);
3330 dir->expanded = TRUE;
3332 /* insert entries in left pane */
3333 insert_entries(&child->left, p, NULL, TF_ALL, idx);
3335 if (!child->header_wdths_ok) {
3336 if (calc_widths(&child->left, FALSE)) {
3337 set_header(&child->left);
3339 child->header_wdths_ok = TRUE;
3343 return TRUE;
3347 static void collapse_entry(Pane* pane, Entry* dir)
3349 int idx;
3351 if (!dir) return;
3352 idx = SendMessageW(pane->hwnd, LB_FINDSTRING, 0, (LPARAM)dir);
3354 ShowWindow(pane->hwnd, SW_HIDE);
3356 /* hide sub entries */
3357 for(;;) {
3358 LRESULT res = SendMessageW(pane->hwnd, LB_GETITEMDATA, idx+1, 0);
3359 Entry* sub = (Entry*) res;
3361 if (res==LB_ERR || !sub || sub->level<=dir->level)
3362 break;
3364 SendMessageW(pane->hwnd, LB_DELETESTRING, idx+1, 0);
3367 dir->expanded = FALSE;
3369 ShowWindow(pane->hwnd, SW_SHOW);
3373 static void refresh_right_pane(ChildWnd* child)
3375 SendMessageW(child->right.hwnd, LB_RESETCONTENT, 0, 0);
3376 insert_entries(&child->right, child->right.root, child->filter_pattern, child->filter_flags, -1);
3377 calc_widths(&child->right, FALSE);
3379 set_header(&child->right);
3382 static void set_curdir(ChildWnd* child, Entry* entry, int idx, HWND hwnd)
3384 WCHAR path[MAX_PATH];
3386 if (!entry)
3387 return;
3389 path[0] = '\0';
3391 child->left.cur = entry;
3393 child->right.root = entry->down? entry->down: entry;
3394 child->right.cur = entry;
3396 if (!entry->scanned)
3397 scan_entry(child, entry, idx, hwnd);
3398 else
3399 refresh_right_pane(child);
3401 get_path(entry, path);
3402 lstrcpyW(child->path, path);
3404 if (child->hwnd) /* only change window title, if the window already exists */
3405 SetWindowTextW(child->hwnd, path);
3407 if (path[0])
3408 if (SetCurrentDirectoryW(path))
3409 set_space_status();
3413 static void refresh_child(ChildWnd* child)
3415 WCHAR path[MAX_PATH], drv[_MAX_DRIVE+1];
3416 Entry* entry;
3417 int idx;
3419 get_path(child->left.cur, path);
3420 _wsplitpath(path, drv, NULL, NULL, NULL);
3422 child->right.root = NULL;
3424 scan_entry(child, &child->root.entry, 0, child->hwnd);
3426 if (child->root.entry.etype == ET_SHELL)
3428 LPITEMIDLIST local_pidl = get_path_pidl(path,child->hwnd);
3429 if (local_pidl)
3430 entry = read_tree(&child->root, NULL, local_pidl , drv, child->sortOrder, child->hwnd);
3431 else
3432 entry = NULL;
3434 else
3435 entry = read_tree(&child->root, path, NULL, drv, child->sortOrder, child->hwnd);
3437 if (!entry)
3438 entry = &child->root.entry;
3440 insert_entries(&child->left, child->root.entry.down, NULL, TF_ALL, 0);
3442 set_curdir(child, entry, 0, child->hwnd);
3444 idx = SendMessageW(child->left.hwnd, LB_FINDSTRING, 0, (LPARAM)child->left.cur);
3445 SendMessageW(child->left.hwnd, LB_SETCURSEL, idx, 0);
3449 static void create_drive_bar(void)
3451 TBBUTTON drivebarBtn = {0, 0, TBSTATE_ENABLED, BTNS_BUTTON, {0, 0}, 0, 0};
3452 WCHAR b1[BUFFER_LEN];
3453 int btn = 1;
3454 PWSTR p;
3456 GetLogicalDriveStringsW(BUFFER_LEN, Globals.drives);
3458 Globals.hdrivebar = CreateToolbarEx(Globals.hMainWnd, WS_CHILD|WS_VISIBLE|CCS_NOMOVEY|TBSTYLE_LIST,
3459 IDW_DRIVEBAR, 2, Globals.hInstance, IDB_DRIVEBAR, &drivebarBtn,
3460 0, 16, 13, 16, 13, sizeof(TBBUTTON));
3462 #ifdef __WINE__
3463 /* insert unix file system button */
3464 b1[0] = '/';
3465 b1[1] = '\0';
3466 b1[2] = '\0';
3467 SendMessageW(Globals.hdrivebar, TB_ADDSTRINGW, 0, (LPARAM)b1);
3469 drivebarBtn.idCommand = ID_DRIVE_UNIX_FS;
3470 SendMessageW(Globals.hdrivebar, TB_INSERTBUTTONW, btn++, (LPARAM)&drivebarBtn);
3471 drivebarBtn.iString++;
3472 #endif
3473 /* insert shell namespace button */
3474 load_string(b1, sizeof(b1)/sizeof(b1[0]), IDS_SHELL);
3475 b1[lstrlenW(b1)+1] = '\0';
3476 SendMessageW(Globals.hdrivebar, TB_ADDSTRINGW, 0, (LPARAM)b1);
3478 drivebarBtn.idCommand = ID_DRIVE_SHELL_NS;
3479 SendMessageW(Globals.hdrivebar, TB_INSERTBUTTONW, btn++, (LPARAM)&drivebarBtn);
3480 drivebarBtn.iString++;
3482 /* register windows drive root strings */
3483 SendMessageW(Globals.hdrivebar, TB_ADDSTRINGW, 0, (LPARAM)Globals.drives);
3485 drivebarBtn.idCommand = ID_DRIVE_FIRST;
3487 for(p=Globals.drives; *p; ) {
3488 switch(GetDriveTypeW(p)) {
3489 case DRIVE_REMOVABLE: drivebarBtn.iBitmap = 1; break;
3490 case DRIVE_CDROM: drivebarBtn.iBitmap = 3; break;
3491 case DRIVE_REMOTE: drivebarBtn.iBitmap = 4; break;
3492 case DRIVE_RAMDISK: drivebarBtn.iBitmap = 5; break;
3493 default:/*DRIVE_FIXED*/ drivebarBtn.iBitmap = 2;
3496 SendMessageW(Globals.hdrivebar, TB_INSERTBUTTONW, btn++, (LPARAM)&drivebarBtn);
3497 drivebarBtn.idCommand++;
3498 drivebarBtn.iString++;
3500 while(*p++);
3504 static void refresh_drives(void)
3506 RECT rect;
3508 /* destroy drive bar */
3509 DestroyWindow(Globals.hdrivebar);
3510 Globals.hdrivebar = 0;
3512 /* re-create drive bar */
3513 create_drive_bar();
3515 /* update window layout */
3516 GetClientRect(Globals.hMainWnd, &rect);
3517 SendMessageW(Globals.hMainWnd, WM_SIZE, 0, MAKELONG(rect.right, rect.bottom));
3521 static BOOL launch_file(HWND hwnd, LPCWSTR cmd, UINT nCmdShow)
3523 HINSTANCE hinst = ShellExecuteW(hwnd, NULL/*operation*/, cmd, NULL/*parameters*/, NULL/*dir*/, nCmdShow);
3525 if (PtrToUlong(hinst) <= 32) {
3526 display_error(hwnd, GetLastError());
3527 return FALSE;
3530 return TRUE;
3534 static BOOL launch_entry(Entry* entry, HWND hwnd, UINT nCmdShow)
3536 WCHAR cmd[MAX_PATH];
3538 if (entry->etype == ET_SHELL) {
3539 BOOL ret = TRUE;
3541 SHELLEXECUTEINFOW shexinfo;
3543 shexinfo.cbSize = sizeof(SHELLEXECUTEINFOW);
3544 shexinfo.fMask = SEE_MASK_IDLIST;
3545 shexinfo.hwnd = hwnd;
3546 shexinfo.lpVerb = NULL;
3547 shexinfo.lpFile = NULL;
3548 shexinfo.lpParameters = NULL;
3549 shexinfo.lpDirectory = NULL;
3550 shexinfo.nShow = nCmdShow;
3551 shexinfo.lpIDList = get_to_absolute_pidl(entry, hwnd);
3553 if (!ShellExecuteExW(&shexinfo)) {
3554 display_error(hwnd, GetLastError());
3555 ret = FALSE;
3558 if (shexinfo.lpIDList != entry->pidl)
3559 IMalloc_Free(Globals.iMalloc, shexinfo.lpIDList);
3561 return ret;
3564 get_path(entry, cmd);
3566 /* start program, open document... */
3567 return launch_file(hwnd, cmd, nCmdShow);
3571 static void activate_entry(ChildWnd* child, Pane* pane, HWND hwnd)
3573 Entry* entry = pane->cur;
3575 if (!entry)
3576 return;
3578 if (entry->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
3579 int scanned_old = entry->scanned;
3581 if (!scanned_old)
3583 int idx = SendMessageW(child->left.hwnd, LB_GETCURSEL, 0, 0);
3584 scan_entry(child, entry, idx, hwnd);
3587 if (entry->data.cFileName[0]=='.' && entry->data.cFileName[1]=='\0')
3588 return;
3590 if (entry->data.cFileName[0]=='.' && entry->data.cFileName[1]=='.' && entry->data.cFileName[2]=='\0') {
3591 entry = child->left.cur->up;
3592 collapse_entry(&child->left, entry);
3593 goto focus_entry;
3594 } else if (entry->expanded)
3595 collapse_entry(pane, child->left.cur);
3596 else {
3597 expand_entry(child, child->left.cur);
3599 if (!pane->treePane) focus_entry: {
3600 int idxstart = SendMessageW(child->left.hwnd, LB_GETCURSEL, 0, 0);
3601 int idx = SendMessageW(child->left.hwnd, LB_FINDSTRING, idxstart, (LPARAM)entry);
3602 SendMessageW(child->left.hwnd, LB_SETCURSEL, idx, 0);
3603 set_curdir(child, entry, idx, hwnd);
3607 if (!scanned_old) {
3608 calc_widths(pane, FALSE);
3610 set_header(pane);
3612 } else {
3613 if (GetKeyState(VK_MENU) < 0)
3614 show_properties_dlg(entry, child->hwnd);
3615 else
3616 launch_entry(entry, child->hwnd, SW_SHOWNORMAL);
3621 static BOOL pane_command(Pane* pane, UINT cmd)
3623 switch(cmd) {
3624 case ID_VIEW_NAME:
3625 if (pane->visible_cols) {
3626 pane->visible_cols = 0;
3627 calc_widths(pane, TRUE);
3628 set_header(pane);
3629 InvalidateRect(pane->hwnd, 0, TRUE);
3630 CheckMenuItem(Globals.hMenuView, ID_VIEW_NAME, MF_BYCOMMAND|MF_CHECKED);
3631 CheckMenuItem(Globals.hMenuView, ID_VIEW_ALL_ATTRIBUTES, MF_BYCOMMAND);
3633 break;
3635 case ID_VIEW_ALL_ATTRIBUTES:
3636 if (pane->visible_cols != COL_ALL) {
3637 pane->visible_cols = COL_ALL;
3638 calc_widths(pane, TRUE);
3639 set_header(pane);
3640 InvalidateRect(pane->hwnd, 0, TRUE);
3641 CheckMenuItem(Globals.hMenuView, ID_VIEW_NAME, MF_BYCOMMAND);
3642 CheckMenuItem(Globals.hMenuView, ID_VIEW_ALL_ATTRIBUTES, MF_BYCOMMAND|MF_CHECKED);
3644 break;
3646 case ID_PREFERRED_SIZES: {
3647 calc_widths(pane, TRUE);
3648 set_header(pane);
3649 InvalidateRect(pane->hwnd, 0, TRUE);
3650 break;}
3652 /* TODO: more command ids... */
3654 default:
3655 return FALSE;
3658 return TRUE;
3662 static void set_sort_order(ChildWnd* child, SORT_ORDER sortOrder)
3664 if (child->sortOrder != sortOrder) {
3665 child->sortOrder = sortOrder;
3666 refresh_child(child);
3670 static void update_view_menu(ChildWnd* child)
3672 CheckMenuItem(Globals.hMenuView, ID_VIEW_SORT_NAME, child->sortOrder==SORT_NAME? MF_CHECKED: MF_UNCHECKED);
3673 CheckMenuItem(Globals.hMenuView, ID_VIEW_SORT_TYPE, child->sortOrder==SORT_EXT? MF_CHECKED: MF_UNCHECKED);
3674 CheckMenuItem(Globals.hMenuView, ID_VIEW_SORT_SIZE, child->sortOrder==SORT_SIZE? MF_CHECKED: MF_UNCHECKED);
3675 CheckMenuItem(Globals.hMenuView, ID_VIEW_SORT_DATE, child->sortOrder==SORT_DATE? MF_CHECKED: MF_UNCHECKED);
3679 static BOOL is_directory(LPCWSTR target)
3681 /*TODO correctly handle UNIX paths */
3682 DWORD target_attr = GetFileAttributesW(target);
3684 if (target_attr == INVALID_FILE_ATTRIBUTES)
3685 return FALSE;
3687 return (target_attr & FILE_ATTRIBUTE_DIRECTORY) != 0;
3690 static BOOL prompt_target(Pane* pane, LPWSTR source, LPWSTR target)
3692 WCHAR path[MAX_PATH];
3693 int len;
3695 get_path(pane->cur, path);
3697 if (DialogBoxParamW(Globals.hInstance, MAKEINTRESOURCEW(IDD_SELECT_DESTINATION), pane->hwnd, DestinationDlgProc, (LPARAM)path) != IDOK)
3698 return FALSE;
3700 get_path(pane->cur, source);
3702 /* convert relative targets to absolute paths */
3703 if (path[0]!='/' && path[1]!=':') {
3704 get_path(pane->cur->up, target);
3705 len = lstrlenW(target);
3707 if (target[len-1]!='\\' && target[len-1]!='/')
3708 target[len++] = '/';
3710 lstrcpyW(target+len, path);
3711 } else
3712 lstrcpyW(target, path);
3714 /* If the target already exists as directory, create a new target below this. */
3715 if (is_directory(path)) {
3716 WCHAR fname[_MAX_FNAME], ext[_MAX_EXT];
3717 static const WCHAR sAppend[] = {'%','s','/','%','s','%','s','\0'};
3719 _wsplitpath(source, NULL, NULL, fname, ext);
3721 wsprintfW(target, sAppend, path, fname, ext);
3724 return TRUE;
3728 static IContextMenu2* s_pctxmenu2 = NULL;
3729 static IContextMenu3* s_pctxmenu3 = NULL;
3731 static void CtxMenu_reset(void)
3733 s_pctxmenu2 = NULL;
3734 s_pctxmenu3 = NULL;
3737 static IContextMenu* CtxMenu_query_interfaces(IContextMenu* pcm1)
3739 IContextMenu* pcm = NULL;
3741 CtxMenu_reset();
3743 if (IContextMenu_QueryInterface(pcm1, &IID_IContextMenu3, (void**)&pcm) == NOERROR)
3744 s_pctxmenu3 = (LPCONTEXTMENU3)pcm;
3745 else if (IContextMenu_QueryInterface(pcm1, &IID_IContextMenu2, (void**)&pcm) == NOERROR)
3746 s_pctxmenu2 = (LPCONTEXTMENU2)pcm;
3748 if (pcm) {
3749 IContextMenu_Release(pcm1);
3750 return pcm;
3751 } else
3752 return pcm1;
3755 static BOOL CtxMenu_HandleMenuMsg(UINT nmsg, WPARAM wparam, LPARAM lparam)
3757 if (s_pctxmenu3) {
3758 if (SUCCEEDED(IContextMenu3_HandleMenuMsg(s_pctxmenu3, nmsg, wparam, lparam)))
3759 return TRUE;
3762 if (s_pctxmenu2)
3763 if (SUCCEEDED(IContextMenu2_HandleMenuMsg(s_pctxmenu2, nmsg, wparam, lparam)))
3764 return TRUE;
3766 return FALSE;
3769 static HRESULT ShellFolderContextMenu(IShellFolder* shell_folder, HWND hwndParent, int cidl, LPCITEMIDLIST* apidl, int x, int y)
3771 IContextMenu* pcm;
3772 BOOL executed = FALSE;
3774 HRESULT hr = IShellFolder_GetUIObjectOf(shell_folder, hwndParent, cidl, apidl, &IID_IContextMenu, NULL, (LPVOID*)&pcm);
3776 if (SUCCEEDED(hr)) {
3777 HMENU hmenu = CreatePopupMenu();
3779 pcm = CtxMenu_query_interfaces(pcm);
3781 if (hmenu) {
3782 hr = IContextMenu_QueryContextMenu(pcm, hmenu, 0, FCIDM_SHVIEWFIRST, FCIDM_SHVIEWLAST, CMF_NORMAL);
3784 if (SUCCEEDED(hr)) {
3785 UINT idCmd = TrackPopupMenu(hmenu, TPM_LEFTALIGN|TPM_RETURNCMD|TPM_RIGHTBUTTON, x, y, 0, hwndParent, NULL);
3787 CtxMenu_reset();
3789 if (idCmd) {
3790 CMINVOKECOMMANDINFO cmi;
3792 cmi.cbSize = sizeof(CMINVOKECOMMANDINFO);
3793 cmi.fMask = 0;
3794 cmi.hwnd = hwndParent;
3795 cmi.lpVerb = (LPCSTR)(INT_PTR)(idCmd - FCIDM_SHVIEWFIRST);
3796 cmi.lpParameters = NULL;
3797 cmi.lpDirectory = NULL;
3798 cmi.nShow = SW_SHOWNORMAL;
3799 cmi.dwHotKey = 0;
3800 cmi.hIcon = 0;
3802 hr = IContextMenu_InvokeCommand(pcm, &cmi);
3803 executed = TRUE;
3805 } else
3806 CtxMenu_reset();
3809 IContextMenu_Release(pcm);
3812 return FAILED(hr)? hr: executed? S_OK: S_FALSE;
3815 static LRESULT CALLBACK ChildWndProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam)
3817 ChildWnd* child = (ChildWnd*)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
3818 ASSERT(child);
3820 switch(nmsg) {
3821 case WM_DRAWITEM: {
3822 LPDRAWITEMSTRUCT dis = (LPDRAWITEMSTRUCT)lparam;
3823 Entry* entry = (Entry*) dis->itemData;
3825 if (dis->CtlID == IDW_TREE_LEFT)
3826 draw_item(&child->left, dis, entry, -1);
3827 else if (dis->CtlID == IDW_TREE_RIGHT)
3828 draw_item(&child->right, dis, entry, -1);
3829 else
3830 goto draw_menu_item;
3832 return TRUE;}
3834 case WM_CREATE:
3835 InitChildWindow(child);
3836 break;
3838 case WM_NCDESTROY:
3839 free_child_window(child);
3840 SetWindowLongPtrW(hwnd, GWLP_USERDATA, 0);
3841 break;
3843 case WM_PAINT: {
3844 PAINTSTRUCT ps;
3845 HBRUSH lastBrush;
3846 RECT rt;
3847 GetClientRect(hwnd, &rt);
3848 BeginPaint(hwnd, &ps);
3849 rt.left = child->split_pos-SPLIT_WIDTH/2;
3850 rt.right = child->split_pos+SPLIT_WIDTH/2+1;
3851 lastBrush = SelectObject(ps.hdc, GetStockObject(COLOR_SPLITBAR));
3852 Rectangle(ps.hdc, rt.left, rt.top-1, rt.right, rt.bottom+1);
3853 SelectObject(ps.hdc, lastBrush);
3854 EndPaint(hwnd, &ps);
3855 break;}
3857 case WM_SETCURSOR:
3858 if (LOWORD(lparam) == HTCLIENT) {
3859 POINT pt;
3860 GetCursorPos(&pt);
3861 ScreenToClient(hwnd, &pt);
3863 if (pt.x>=child->split_pos-SPLIT_WIDTH/2 && pt.x<child->split_pos+SPLIT_WIDTH/2+1) {
3864 SetCursor(LoadCursorW(0, (LPCWSTR)IDC_SIZEWE));
3865 return TRUE;
3868 goto def;
3870 case WM_LBUTTONDOWN: {
3871 RECT rt;
3872 int x = (short)LOWORD(lparam);
3874 GetClientRect(hwnd, &rt);
3876 if (x>=child->split_pos-SPLIT_WIDTH/2 && x<child->split_pos+SPLIT_WIDTH/2+1) {
3877 last_split = child->split_pos;
3878 SetCapture(hwnd);
3881 break;}
3883 case WM_LBUTTONUP:
3884 if (GetCapture() == hwnd)
3885 ReleaseCapture();
3886 break;
3888 case WM_KEYDOWN:
3889 if (wparam == VK_ESCAPE)
3890 if (GetCapture() == hwnd) {
3891 RECT rt;
3892 child->split_pos = last_split;
3893 GetClientRect(hwnd, &rt);
3894 resize_tree(child, rt.right, rt.bottom);
3895 last_split = -1;
3896 ReleaseCapture();
3897 SetCursor(LoadCursorW(0, (LPCWSTR)IDC_ARROW));
3899 break;
3901 case WM_MOUSEMOVE:
3902 if (GetCapture() == hwnd) {
3903 RECT rt;
3904 int x = (short)LOWORD(lparam);
3906 GetClientRect(hwnd, &rt);
3908 if (x>=0 && x<rt.right) {
3909 child->split_pos = x;
3910 resize_tree(child, rt.right, rt.bottom);
3911 rt.left = x-SPLIT_WIDTH/2;
3912 rt.right = x+SPLIT_WIDTH/2+1;
3913 InvalidateRect(hwnd, &rt, FALSE);
3914 UpdateWindow(child->left.hwnd);
3915 UpdateWindow(hwnd);
3916 UpdateWindow(child->right.hwnd);
3919 break;
3921 case WM_GETMINMAXINFO:
3922 DefMDIChildProcW(hwnd, nmsg, wparam, lparam);
3924 {LPMINMAXINFO lpmmi = (LPMINMAXINFO)lparam;
3926 lpmmi->ptMaxTrackSize.x <<= 1;/*2*GetSystemMetrics(SM_CXSCREEN) / SM_CXVIRTUALSCREEN */
3927 lpmmi->ptMaxTrackSize.y <<= 1;/*2*GetSystemMetrics(SM_CYSCREEN) / SM_CYVIRTUALSCREEN */
3928 break;}
3930 case WM_SETFOCUS:
3931 if (SetCurrentDirectoryW(child->path))
3932 set_space_status();
3933 SetFocus(child->focus_pane? child->right.hwnd: child->left.hwnd);
3934 break;
3936 case WM_DISPATCH_COMMAND: {
3937 Pane* pane = GetFocus()==child->left.hwnd? &child->left: &child->right;
3939 switch(LOWORD(wparam)) {
3940 case ID_WINDOW_NEW: {
3941 ChildWnd* new_child = alloc_child_window(child->path, NULL, hwnd);
3943 if (!create_child_window(new_child))
3944 HeapFree(GetProcessHeap(), 0, new_child);
3946 break;}
3948 case ID_REFRESH:
3949 refresh_drives();
3950 refresh_child(child);
3951 break;
3953 case ID_ACTIVATE:
3954 activate_entry(child, pane, hwnd);
3955 break;
3957 case ID_FILE_MOVE: {
3958 WCHAR source[BUFFER_LEN], target[BUFFER_LEN];
3960 if (prompt_target(pane, source, target)) {
3961 SHFILEOPSTRUCTW shfo = {hwnd, FO_MOVE, source, target};
3963 source[lstrlenW(source)+1] = '\0';
3964 target[lstrlenW(target)+1] = '\0';
3966 if (!SHFileOperationW(&shfo))
3967 refresh_child(child);
3969 break;}
3971 case ID_FILE_COPY: {
3972 WCHAR source[BUFFER_LEN], target[BUFFER_LEN];
3974 if (prompt_target(pane, source, target)) {
3975 SHFILEOPSTRUCTW shfo = {hwnd, FO_COPY, source, target};
3977 source[lstrlenW(source)+1] = '\0';
3978 target[lstrlenW(target)+1] = '\0';
3980 if (!SHFileOperationW(&shfo))
3981 refresh_child(child);
3983 break;}
3985 case ID_FILE_DELETE: {
3986 WCHAR path[BUFFER_LEN];
3987 SHFILEOPSTRUCTW shfo = {hwnd, FO_DELETE, path, NULL, FOF_ALLOWUNDO};
3989 get_path(pane->cur, path);
3991 path[lstrlenW(path)+1] = '\0';
3993 if (!SHFileOperationW(&shfo))
3994 refresh_child(child);
3995 break;}
3997 case ID_VIEW_SORT_NAME:
3998 set_sort_order(child, SORT_NAME);
3999 break;
4001 case ID_VIEW_SORT_TYPE:
4002 set_sort_order(child, SORT_EXT);
4003 break;
4005 case ID_VIEW_SORT_SIZE:
4006 set_sort_order(child, SORT_SIZE);
4007 break;
4009 case ID_VIEW_SORT_DATE:
4010 set_sort_order(child, SORT_DATE);
4011 break;
4013 case ID_VIEW_FILTER: {
4014 struct FilterDialog dlg;
4016 memset(&dlg, 0, sizeof(struct FilterDialog));
4017 lstrcpyW(dlg.pattern, child->filter_pattern);
4018 dlg.flags = child->filter_flags;
4020 if (DialogBoxParamW(Globals.hInstance, MAKEINTRESOURCEW(IDD_DIALOG_VIEW_TYPE), hwnd, FilterDialogDlgProc, (LPARAM)&dlg) == IDOK) {
4021 lstrcpyW(child->filter_pattern, dlg.pattern);
4022 child->filter_flags = dlg.flags;
4023 refresh_right_pane(child);
4025 break;}
4027 case ID_VIEW_SPLIT: {
4028 last_split = child->split_pos;
4029 SetCapture(hwnd);
4030 break;}
4032 case ID_EDIT_PROPERTIES:
4033 show_properties_dlg(pane->cur, child->hwnd);
4034 break;
4036 default:
4037 return pane_command(pane, LOWORD(wparam));
4040 return TRUE;}
4042 case WM_COMMAND: {
4043 Pane* pane = GetFocus()==child->left.hwnd? &child->left: &child->right;
4045 switch(HIWORD(wparam)) {
4046 case LBN_SELCHANGE: {
4047 int idx = SendMessageW(pane->hwnd, LB_GETCURSEL, 0, 0);
4048 Entry* entry = (Entry*)SendMessageW(pane->hwnd, LB_GETITEMDATA, idx, 0);
4050 if (pane == &child->left)
4051 set_curdir(child, entry, idx, hwnd);
4052 else
4053 pane->cur = entry;
4054 break;}
4056 case LBN_DBLCLK:
4057 activate_entry(child, pane, hwnd);
4058 break;
4060 break;}
4062 case WM_NOTIFY: {
4063 NMHDR* pnmh = (NMHDR*) lparam;
4064 return pane_notify(pnmh->idFrom==IDW_HEADER_LEFT? &child->left: &child->right, pnmh);}
4066 case WM_CONTEXTMENU: {
4067 POINT pt, pt_clnt;
4068 Pane* pane;
4069 int idx;
4071 /* first select the current item in the listbox */
4072 HWND hpanel = (HWND) wparam;
4073 pt_clnt.x = pt.x = (short)LOWORD(lparam);
4074 pt_clnt.y = pt.y = (short)HIWORD(lparam);
4075 ScreenToClient(hpanel, &pt_clnt);
4076 SendMessageW(hpanel, WM_LBUTTONDOWN, 0, MAKELONG(pt_clnt.x, pt_clnt.y));
4077 SendMessageW(hpanel, WM_LBUTTONUP, 0, MAKELONG(pt_clnt.x, pt_clnt.y));
4079 /* now create the popup menu using shell namespace and IContextMenu */
4080 pane = GetFocus()==child->left.hwnd? &child->left: &child->right;
4081 idx = SendMessageW(pane->hwnd, LB_GETCURSEL, 0, 0);
4083 if (idx != -1) {
4084 Entry* entry = (Entry*)SendMessageW(pane->hwnd, LB_GETITEMDATA, idx, 0);
4086 LPITEMIDLIST pidl_abs = get_to_absolute_pidl(entry, hwnd);
4088 if (pidl_abs) {
4089 IShellFolder* parentFolder;
4090 LPCITEMIDLIST pidlLast;
4092 /* get and use the parent folder to display correct context menu in all cases */
4093 if (SUCCEEDED(SHBindToParent(pidl_abs, &IID_IShellFolder, (LPVOID*)&parentFolder, &pidlLast))) {
4094 if (ShellFolderContextMenu(parentFolder, hwnd, 1, &pidlLast, pt.x, pt.y) == S_OK)
4095 refresh_child(child);
4097 IShellFolder_Release(parentFolder);
4100 IMalloc_Free(Globals.iMalloc, pidl_abs);
4103 break;}
4105 case WM_MEASUREITEM:
4106 draw_menu_item:
4107 if (!wparam) /* Is the message menu-related? */
4108 if (CtxMenu_HandleMenuMsg(nmsg, wparam, lparam))
4109 return TRUE;
4111 break;
4113 case WM_INITMENUPOPUP:
4114 if (CtxMenu_HandleMenuMsg(nmsg, wparam, lparam))
4115 return 0;
4117 update_view_menu(child);
4118 break;
4120 case WM_MENUCHAR: /* only supported by IContextMenu3 */
4121 if (s_pctxmenu3) {
4122 LRESULT lResult = 0;
4124 IContextMenu3_HandleMenuMsg2(s_pctxmenu3, nmsg, wparam, lparam, &lResult);
4126 return lResult;
4129 break;
4131 case WM_SIZE:
4132 if (wparam != SIZE_MINIMIZED)
4133 resize_tree(child, LOWORD(lparam), HIWORD(lparam));
4134 /* fall through */
4136 default: def:
4137 return DefMDIChildProcW(hwnd, nmsg, wparam, lparam);
4140 return 0;
4144 static LRESULT CALLBACK TreeWndProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam)
4146 ChildWnd* child = (ChildWnd*)GetWindowLongPtrW(GetParent(hwnd), GWLP_USERDATA);
4147 Pane* pane = (Pane*)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
4148 ASSERT(child);
4150 switch(nmsg) {
4151 case WM_HSCROLL:
4152 set_header(pane);
4153 break;
4155 case WM_SETFOCUS:
4156 child->focus_pane = pane==&child->right? 1: 0;
4157 SendMessageW(hwnd, LB_SETSEL, TRUE, 1);
4158 /*TODO: check menu items */
4159 break;
4161 case WM_KEYDOWN:
4162 if (wparam == VK_TAB) {
4163 /*TODO: SetFocus(Globals.hdrivebar) */
4164 SetFocus(child->focus_pane? child->left.hwnd: child->right.hwnd);
4168 return CallWindowProcW(g_orgTreeWndProc, hwnd, nmsg, wparam, lparam);
4172 static void InitInstance(HINSTANCE hinstance)
4174 static const WCHAR sFont[] = {'M','i','c','r','o','s','o','f','t',' ','S','a','n','s',' ','S','e','r','i','f','\0'};
4176 WNDCLASSEXW wcFrame;
4177 WNDCLASSW wcChild;
4178 int col;
4180 INITCOMMONCONTROLSEX icc = {
4181 sizeof(INITCOMMONCONTROLSEX),
4182 ICC_BAR_CLASSES
4185 HDC hdc = GetDC(0);
4187 setlocale(LC_COLLATE, ""); /* set collating rules to local settings for compareName */
4189 InitCommonControlsEx(&icc);
4192 /* register frame window class */
4194 wcFrame.cbSize = sizeof(WNDCLASSEXW);
4195 wcFrame.style = 0;
4196 wcFrame.lpfnWndProc = FrameWndProc;
4197 wcFrame.cbClsExtra = 0;
4198 wcFrame.cbWndExtra = 0;
4199 wcFrame.hInstance = hinstance;
4200 wcFrame.hIcon = LoadIconW(hinstance, MAKEINTRESOURCEW(IDI_WINEFILE));
4201 wcFrame.hCursor = LoadCursorW(0, (LPCWSTR)IDC_ARROW);
4202 wcFrame.hbrBackground = 0;
4203 wcFrame.lpszMenuName = 0;
4204 wcFrame.lpszClassName = sWINEFILEFRAME;
4205 wcFrame.hIconSm = LoadImageW(hinstance, MAKEINTRESOURCEW(IDI_WINEFILE), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED);
4207 Globals.hframeClass = RegisterClassExW(&wcFrame);
4210 /* register tree windows class */
4212 wcChild.style = CS_CLASSDC|CS_DBLCLKS|CS_VREDRAW;
4213 wcChild.lpfnWndProc = ChildWndProc;
4214 wcChild.cbClsExtra = 0;
4215 wcChild.cbWndExtra = 0;
4216 wcChild.hInstance = hinstance;
4217 wcChild.hIcon = LoadIconW(hinstance, MAKEINTRESOURCEW(IDI_WINEFILE));
4218 wcChild.hCursor = LoadCursorW(0, (LPCWSTR)IDC_ARROW);
4219 wcChild.hbrBackground = 0;
4220 wcChild.lpszMenuName = 0;
4221 wcChild.lpszClassName = sWINEFILETREE;
4223 RegisterClassW(&wcChild);
4226 Globals.haccel = LoadAcceleratorsW(hinstance, MAKEINTRESOURCEW(IDA_WINEFILE));
4228 Globals.hfont = CreateFontW(-MulDiv(8,GetDeviceCaps(hdc,LOGPIXELSY),72), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, sFont);
4230 ReleaseDC(0, hdc);
4232 Globals.hInstance = hinstance;
4234 CoInitialize(NULL);
4235 CoGetMalloc(MEMCTX_TASK, &Globals.iMalloc);
4236 SHGetDesktopFolder(&Globals.iDesktop);
4237 Globals.cfStrFName = RegisterClipboardFormatW(CFSTR_FILENAMEW);
4239 /* load column strings */
4240 col = 1;
4242 load_string(g_pos_names[col++], sizeof(g_pos_names[col])/sizeof(g_pos_names[col][0]), IDS_COL_NAME);
4243 load_string(g_pos_names[col++], sizeof(g_pos_names[col])/sizeof(g_pos_names[col][0]), IDS_COL_SIZE);
4244 load_string(g_pos_names[col++], sizeof(g_pos_names[col])/sizeof(g_pos_names[col][0]), IDS_COL_CDATE);
4245 load_string(g_pos_names[col++], sizeof(g_pos_names[col])/sizeof(g_pos_names[col][0]), IDS_COL_ADATE);
4246 load_string(g_pos_names[col++], sizeof(g_pos_names[col])/sizeof(g_pos_names[col][0]), IDS_COL_MDATE);
4247 load_string(g_pos_names[col++], sizeof(g_pos_names[col])/sizeof(g_pos_names[col][0]), IDS_COL_IDX);
4248 load_string(g_pos_names[col++], sizeof(g_pos_names[col])/sizeof(g_pos_names[col][0]), IDS_COL_LINKS);
4249 load_string(g_pos_names[col++], sizeof(g_pos_names[col])/sizeof(g_pos_names[col][0]), IDS_COL_ATTR);
4250 load_string(g_pos_names[col++], sizeof(g_pos_names[col])/sizeof(g_pos_names[col][0]), IDS_COL_SEC);
4254 static BOOL show_frame(HWND hwndParent, int cmdshow, LPCWSTR path)
4256 static const WCHAR sMDICLIENT[] = {'M','D','I','C','L','I','E','N','T','\0'};
4258 WCHAR buffer[MAX_PATH], b1[BUFFER_LEN];
4259 ChildWnd* child;
4260 HMENU hMenuFrame, hMenuWindow;
4261 windowOptions opts;
4263 CLIENTCREATESTRUCT ccs;
4265 if (Globals.hMainWnd)
4266 return TRUE;
4268 opts = load_registry_settings();
4269 hMenuFrame = LoadMenuW(Globals.hInstance, MAKEINTRESOURCEW(IDM_WINEFILE));
4270 hMenuWindow = GetSubMenu(hMenuFrame, GetMenuItemCount(hMenuFrame)-2);
4272 Globals.hMenuFrame = hMenuFrame;
4273 Globals.hMenuView = GetSubMenu(hMenuFrame, 2);
4274 Globals.hMenuOptions = GetSubMenu(hMenuFrame, 3);
4276 ccs.hWindowMenu = hMenuWindow;
4277 ccs.idFirstChild = IDW_FIRST_CHILD;
4280 /* create main window */
4281 Globals.hMainWnd = CreateWindowExW(0, MAKEINTRESOURCEW(Globals.hframeClass), RS(b1,IDS_WINEFILE), WS_OVERLAPPEDWINDOW,
4282 opts.start_x, opts.start_y, opts.width, opts.height,
4283 hwndParent, Globals.hMenuFrame, Globals.hInstance, 0/*lpParam*/);
4286 Globals.hmdiclient = CreateWindowExW(0, sMDICLIENT, NULL,
4287 WS_CHILD|WS_CLIPCHILDREN|WS_VSCROLL|WS_HSCROLL|WS_VISIBLE|WS_BORDER,
4288 0, 0, 0, 0,
4289 Globals.hMainWnd, 0, Globals.hInstance, &ccs);
4291 CheckMenuItem(Globals.hMenuOptions, ID_VIEW_DRIVE_BAR, MF_BYCOMMAND|MF_CHECKED);
4292 CheckMenuItem(Globals.hMenuOptions, ID_VIEW_SAVESETTINGS, MF_BYCOMMAND);
4294 create_drive_bar();
4297 TBBUTTON toolbarBtns[] = {
4298 {0, 0, 0, BTNS_SEP, {0, 0}, 0, 0},
4299 {0, ID_WINDOW_NEW, TBSTATE_ENABLED, BTNS_BUTTON, {0, 0}, 0, 0},
4300 {1, ID_WINDOW_CASCADE, TBSTATE_ENABLED, BTNS_BUTTON, {0, 0}, 0, 0},
4301 {2, ID_WINDOW_TILE_HORZ, TBSTATE_ENABLED, BTNS_BUTTON, {0, 0}, 0, 0},
4302 {3, ID_WINDOW_TILE_VERT, TBSTATE_ENABLED, BTNS_BUTTON, {0, 0}, 0, 0},
4305 Globals.htoolbar = CreateToolbarEx(Globals.hMainWnd, WS_CHILD|WS_VISIBLE,
4306 IDW_TOOLBAR, 2, Globals.hInstance, IDB_TOOLBAR, toolbarBtns,
4307 sizeof(toolbarBtns)/sizeof(TBBUTTON), 16, 15, 16, 15, sizeof(TBBUTTON));
4308 CheckMenuItem(Globals.hMenuOptions, ID_VIEW_TOOL_BAR, MF_BYCOMMAND|MF_CHECKED);
4311 Globals.hstatusbar = CreateStatusWindowW(WS_CHILD|WS_VISIBLE, 0, Globals.hMainWnd, IDW_STATUSBAR);
4312 CheckMenuItem(Globals.hMenuOptions, ID_VIEW_STATUSBAR, MF_BYCOMMAND|MF_CHECKED);
4314 /*TODO: read paths from registry */
4316 if (!path || !*path) {
4317 GetCurrentDirectoryW(MAX_PATH, buffer);
4318 path = buffer;
4321 ShowWindow(Globals.hMainWnd, cmdshow);
4323 #ifndef __WINE__
4324 /* Shell Namespace as default: */
4325 child = alloc_child_window(path, get_path_pidl(path,Globals.hMainWnd), Globals.hMainWnd);
4326 #else
4327 child = alloc_child_window(path, NULL, Globals.hMainWnd);
4328 #endif
4330 child->pos.showCmd = SW_SHOWMAXIMIZED;
4331 child->pos.rcNormalPosition.left = 0;
4332 child->pos.rcNormalPosition.top = 0;
4333 child->pos.rcNormalPosition.right = 320;
4334 child->pos.rcNormalPosition.bottom = 280;
4336 if (!create_child_window(child)) {
4337 HeapFree(GetProcessHeap(), 0, child);
4338 return FALSE;
4341 SetWindowPlacement(child->hwnd, &child->pos);
4343 Globals.himl = ImageList_LoadImageW(Globals.hInstance, MAKEINTRESOURCEW(IDB_IMAGES), 16, 0, RGB(0,255,0), IMAGE_BITMAP, 0);
4345 Globals.prescan_node = FALSE;
4347 UpdateWindow(Globals.hMainWnd);
4349 if (child->hwnd && path && path[0])
4351 int index,count;
4352 WCHAR drv[_MAX_DRIVE+1], dir[_MAX_DIR], name[_MAX_FNAME], ext[_MAX_EXT];
4353 WCHAR fullname[_MAX_FNAME+_MAX_EXT+1];
4355 memset(name,0,sizeof(name));
4356 memset(name,0,sizeof(ext));
4357 _wsplitpath(path, drv, dir, name, ext);
4358 if (name[0])
4360 count = SendMessageW(child->right.hwnd, LB_GETCOUNT, 0, 0);
4361 lstrcpyW(fullname,name);
4362 lstrcatW(fullname,ext);
4364 for (index = 0; index < count; index ++)
4366 Entry* entry = (Entry*)SendMessageW(child->right.hwnd, LB_GETITEMDATA, index, 0);
4367 if (lstrcmpW(entry->data.cFileName,fullname)==0 ||
4368 lstrcmpW(entry->data.cAlternateFileName,fullname)==0)
4370 SendMessageW(child->right.hwnd, LB_SETCURSEL, index, 0);
4371 SetFocus(child->right.hwnd);
4372 break;
4377 return TRUE;
4380 static void ExitInstance(void)
4382 IShellFolder_Release(Globals.iDesktop);
4383 IMalloc_Release(Globals.iMalloc);
4384 CoUninitialize();
4386 DeleteObject(Globals.hfont);
4387 ImageList_Destroy(Globals.himl);
4390 int APIENTRY wWinMain(HINSTANCE hinstance, HINSTANCE previnstance, LPWSTR cmdline, int cmdshow)
4392 MSG msg;
4394 InitInstance(hinstance);
4396 if( !show_frame(0, cmdshow, cmdline) )
4398 ExitInstance();
4399 return 1;
4402 while(GetMessageW(&msg, 0, 0, 0)) {
4403 if (Globals.hmdiclient && TranslateMDISysAccel(Globals.hmdiclient, &msg))
4404 continue;
4406 if (Globals.hMainWnd && TranslateAcceleratorW(Globals.hMainWnd, Globals.haccel, &msg))
4407 continue;
4409 TranslateMessage(&msg);
4410 DispatchMessageW(&msg);
4413 ExitInstance();
4415 return msg.wParam;