user32/tests: Add tests for minimizing MDI windows.
[wine.git] / programs / winefile / winefile.c
blob27079bbcf848034bcbebfa6277d1938f92bdec1f
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 widths_shown[COLUMNS];
122 int positions[COLUMNS+1];
124 BOOL treePane;
125 int visible_cols;
126 Entry* root;
127 Entry* cur;
128 } Pane;
130 typedef struct {
131 HWND hwnd;
132 Pane left;
133 Pane right;
134 int focus_pane; /* 0: left 1: right */
135 WINDOWPLACEMENT pos;
136 int split_pos;
137 BOOL header_wdths_ok;
139 WCHAR path[MAX_PATH];
140 WCHAR filter_pattern[MAX_PATH];
141 int filter_flags;
142 Root root;
144 SORT_ORDER sortOrder;
145 } ChildWnd;
149 static void read_directory(Entry* dir, LPCWSTR path, SORT_ORDER sortOrder, HWND hwnd);
150 static void set_curdir(ChildWnd* child, Entry* entry, int idx, HWND hwnd);
151 static void refresh_child(ChildWnd* child);
152 static void refresh_drives(void);
153 static void get_path(Entry* dir, PWSTR path);
154 static void format_date(const FILETIME* ft, WCHAR* buffer, int visible_cols);
156 static LRESULT CALLBACK FrameWndProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam);
157 static LRESULT CALLBACK ChildWndProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam);
158 static LRESULT CALLBACK TreeWndProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam);
161 /* globals */
162 WINEFILE_GLOBALS Globals;
164 static int last_split;
166 /* some common string constants */
167 static const WCHAR sEmpty[] = {'\0'};
168 static const WCHAR sSpace[] = {' ', '\0'};
169 static const WCHAR sNumFmt[] = {'%','d','\0'};
170 static const WCHAR sQMarks[] = {'?','?','?','\0'};
172 /* window class names */
173 static const WCHAR sWINEFILEFRAME[] = {'W','F','S','_','F','r','a','m','e','\0'};
174 static const WCHAR sWINEFILETREE[] = {'W','F','S','_','T','r','e','e','\0'};
176 static void format_longlong(LPWSTR ret, ULONGLONG val)
178 WCHAR buffer[65], *p = &buffer[64];
180 *p = 0;
181 do {
182 *(--p) = '0' + val % 10;
183 val /= 10;
184 } while (val);
185 lstrcpyW( ret, p );
189 /* load resource string */
190 static LPWSTR load_string(LPWSTR buffer, DWORD size, UINT id)
192 LoadStringW(Globals.hInstance, id, buffer, size);
193 return buffer;
196 #define RS(b, i) load_string(b, sizeof(b)/sizeof(b[0]), i)
199 /* display error message for the specified WIN32 error code */
200 static void display_error(HWND hwnd, DWORD error)
202 WCHAR b1[BUFFER_LEN], b2[BUFFER_LEN];
203 PWSTR msg;
205 if (FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
206 0, error, MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), (PWSTR)&msg, 0, NULL))
207 MessageBoxW(hwnd, msg, RS(b2,IDS_WINEFILE), MB_OK);
208 else
209 MessageBoxW(hwnd, RS(b1,IDS_ERROR), RS(b2,IDS_WINEFILE), MB_OK);
211 LocalFree(msg);
215 /* display network error message using WNetGetLastErrorW() */
216 static void display_network_error(HWND hwnd)
218 WCHAR msg[BUFFER_LEN], provider[BUFFER_LEN], b2[BUFFER_LEN];
219 DWORD error;
221 if (WNetGetLastErrorW(&error, msg, BUFFER_LEN, provider, BUFFER_LEN) == NO_ERROR)
222 MessageBoxW(hwnd, msg, RS(b2,IDS_WINEFILE), MB_OK);
225 static inline BOOL get_check(HWND hwnd, INT id)
227 return BST_CHECKED&SendMessageW(GetDlgItem(hwnd, id), BM_GETSTATE, 0, 0);
230 static inline INT set_check(HWND hwnd, INT id, BOOL on)
232 return SendMessageW(GetDlgItem(hwnd, id), BM_SETCHECK, on?BST_CHECKED:BST_UNCHECKED, 0);
235 static inline void choose_font(HWND hwnd)
237 WCHAR dlg_name[BUFFER_LEN], dlg_info[BUFFER_LEN];
238 CHOOSEFONTW chFont;
239 LOGFONTW lFont;
241 HDC hdc = GetDC(hwnd);
243 GetObjectW(Globals.hfont, sizeof(LOGFONTW), &lFont);
245 chFont.lStructSize = sizeof(CHOOSEFONTW);
246 chFont.hwndOwner = hwnd;
247 chFont.hDC = NULL;
248 chFont.lpLogFont = &lFont;
249 chFont.Flags = CF_SCREENFONTS | CF_FORCEFONTEXIST | CF_LIMITSIZE | CF_NOSCRIPTSEL | CF_INITTOLOGFONTSTRUCT | CF_NOVERTFONTS;
250 chFont.rgbColors = RGB(0,0,0);
251 chFont.lCustData = 0;
252 chFont.lpfnHook = NULL;
253 chFont.lpTemplateName = NULL;
254 chFont.hInstance = Globals.hInstance;
255 chFont.lpszStyle = NULL;
256 chFont.nFontType = SIMULATED_FONTTYPE;
257 chFont.nSizeMin = 0;
258 chFont.nSizeMax = 24;
260 if (ChooseFontW(&chFont)) {
261 HWND childWnd;
262 HFONT hFontOld;
264 DeleteObject(Globals.hfont);
265 Globals.hfont = CreateFontIndirectW(&lFont);
266 hFontOld = SelectObject(hdc, Globals.hfont);
267 GetTextExtentPoint32W(hdc, sSpace, 1, &Globals.spaceSize);
269 /* change font in all open child windows */
270 for(childWnd=GetWindow(Globals.hmdiclient,GW_CHILD); childWnd; childWnd=GetNextWindow(childWnd,GW_HWNDNEXT)) {
271 ChildWnd* child = (ChildWnd*) GetWindowLongPtrW(childWnd, GWLP_USERDATA);
272 SendMessageW(child->left.hwnd, WM_SETFONT, (WPARAM)Globals.hfont, TRUE);
273 SendMessageW(child->right.hwnd, WM_SETFONT, (WPARAM)Globals.hfont, TRUE);
274 SendMessageW(child->left.hwnd, LB_SETITEMHEIGHT, 1, max(Globals.spaceSize.cy,IMAGE_HEIGHT+3));
275 SendMessageW(child->right.hwnd, LB_SETITEMHEIGHT, 1, max(Globals.spaceSize.cy,IMAGE_HEIGHT+3));
276 InvalidateRect(child->left.hwnd, NULL, TRUE);
277 InvalidateRect(child->right.hwnd, NULL, TRUE);
280 SelectObject(hdc, hFontOld);
282 else if (CommDlgExtendedError()) {
283 LoadStringW(Globals.hInstance, IDS_FONT_SEL_DLG_NAME, dlg_name, BUFFER_LEN);
284 LoadStringW(Globals.hInstance, IDS_FONT_SEL_ERROR, dlg_info, BUFFER_LEN);
285 MessageBoxW(hwnd, dlg_info, dlg_name, MB_OK);
288 ReleaseDC(hwnd, hdc);
292 /* allocate and initialise a directory entry */
293 static Entry* alloc_entry(void)
295 Entry* entry = HeapAlloc(GetProcessHeap(), 0, sizeof(Entry));
297 entry->pidl = NULL;
298 entry->folder = NULL;
299 entry->hicon = 0;
301 return entry;
304 /* free a directory entry */
305 static void free_entry(Entry* entry)
307 if (entry->hicon && entry->hicon!=(HICON)-1)
308 DestroyIcon(entry->hicon);
310 if (entry->folder && entry->folder!=Globals.iDesktop)
311 IShellFolder_Release(entry->folder);
313 if (entry->pidl)
314 IMalloc_Free(Globals.iMalloc, entry->pidl);
316 HeapFree(GetProcessHeap(), 0, entry);
319 /* recursively free all child entries */
320 static void free_entries(Entry* dir)
322 Entry *entry, *next=dir->down;
324 if (next) {
325 dir->down = 0;
327 do {
328 entry = next;
329 next = entry->next;
331 free_entries(entry);
332 free_entry(entry);
333 } while(next);
338 static void read_directory_win(Entry* dir, LPCWSTR path)
340 Entry* first_entry = NULL;
341 Entry* last = NULL;
342 Entry* entry;
344 int level = dir->level + 1;
345 WIN32_FIND_DATAW w32fd;
346 HANDLE hFind;
347 HANDLE hFile;
349 WCHAR buffer[MAX_PATH], *p;
350 for(p=buffer; *path; )
351 *p++ = *path++;
353 *p++ = '\\';
354 p[0] = '*';
355 p[1] = '\0';
357 hFind = FindFirstFileW(buffer, &w32fd);
359 if (hFind != INVALID_HANDLE_VALUE) {
360 do {
361 entry = alloc_entry();
363 if (!first_entry)
364 first_entry = entry;
366 if (last)
367 last->next = entry;
369 memcpy(&entry->data, &w32fd, sizeof(WIN32_FIND_DATAW));
370 entry->down = NULL;
371 entry->up = dir;
372 entry->expanded = FALSE;
373 entry->scanned = FALSE;
374 entry->level = level;
375 entry->etype = ET_WINDOWS;
376 entry->bhfi_valid = FALSE;
378 lstrcpyW(p, entry->data.cFileName);
380 hFile = CreateFileW(buffer, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
381 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0);
383 if (hFile != INVALID_HANDLE_VALUE) {
384 if (GetFileInformationByHandle(hFile, &entry->bhfi))
385 entry->bhfi_valid = TRUE;
387 CloseHandle(hFile);
390 last = entry;
391 } while(FindNextFileW(hFind, &w32fd));
393 if (last)
394 last->next = NULL;
396 FindClose(hFind);
399 dir->down = first_entry;
400 dir->scanned = TRUE;
404 static Entry* find_entry_win(Entry* dir, LPCWSTR name)
406 Entry* entry;
408 for(entry=dir->down; entry; entry=entry->next) {
409 LPCWSTR p = name;
410 LPCWSTR q = entry->data.cFileName;
412 do {
413 if (!*p || *p == '\\' || *p == '/')
414 return entry;
415 } while(tolower(*p++) == tolower(*q++));
417 p = name;
418 q = entry->data.cAlternateFileName;
420 do {
421 if (!*p || *p == '\\' || *p == '/')
422 return entry;
423 } while(tolower(*p++) == tolower(*q++));
426 return 0;
430 static Entry* read_tree_win(Root* root, LPCWSTR path, SORT_ORDER sortOrder, HWND hwnd)
432 WCHAR buffer[MAX_PATH];
433 Entry* entry = &root->entry;
434 LPCWSTR s = path;
435 PWSTR d = buffer;
437 HCURSOR old_cursor = SetCursor(LoadCursorW(0, (LPCWSTR)IDC_WAIT));
439 entry->etype = ET_WINDOWS;
440 while(entry) {
441 while(*s && *s != '\\' && *s != '/')
442 *d++ = *s++;
444 while(*s == '\\' || *s == '/')
445 s++;
447 *d++ = '\\';
448 *d = '\0';
450 read_directory(entry, buffer, sortOrder, hwnd);
452 if (entry->down)
453 entry->expanded = TRUE;
455 if (!*s)
456 break;
458 entry = find_entry_win(entry, s);
461 SetCursor(old_cursor);
463 return entry;
467 #ifdef __WINE__
469 static BOOL time_to_filetime(time_t t, FILETIME* ftime)
471 struct tm* tm = gmtime(&t);
472 SYSTEMTIME stime;
474 if (!tm)
475 return FALSE;
477 stime.wYear = tm->tm_year+1900;
478 stime.wMonth = tm->tm_mon+1;
479 /* stime.wDayOfWeek */
480 stime.wDay = tm->tm_mday;
481 stime.wHour = tm->tm_hour;
482 stime.wMinute = tm->tm_min;
483 stime.wSecond = tm->tm_sec;
484 stime.wMilliseconds = 0;
486 return SystemTimeToFileTime(&stime, ftime);
489 static void read_directory_unix(Entry* dir, LPCWSTR path)
491 Entry* first_entry = NULL;
492 Entry* last = NULL;
493 Entry* entry;
494 DIR* pdir;
496 int level = dir->level + 1;
497 char cpath[MAX_PATH];
499 WideCharToMultiByte(CP_UNIXCP, 0, path, -1, cpath, MAX_PATH, NULL, NULL);
500 pdir = opendir(cpath);
502 if (pdir) {
503 struct stat st;
504 struct dirent* ent;
505 char buffer[MAX_PATH], *p;
506 const char* s;
508 for(p=buffer,s=cpath; *s; )
509 *p++ = *s++;
511 if (p==buffer || p[-1]!='/')
512 *p++ = '/';
514 while((ent=readdir(pdir))) {
515 entry = alloc_entry();
517 if (!first_entry)
518 first_entry = entry;
520 if (last)
521 last->next = entry;
523 entry->etype = ET_UNIX;
525 strcpy(p, ent->d_name);
526 MultiByteToWideChar(CP_UNIXCP, 0, p, -1, entry->data.cFileName, MAX_PATH);
528 if (!stat(buffer, &st)) {
529 entry->data.dwFileAttributes = p[0]=='.'? FILE_ATTRIBUTE_HIDDEN: 0;
531 if (S_ISDIR(st.st_mode))
532 entry->data.dwFileAttributes |= FILE_ATTRIBUTE_DIRECTORY;
534 entry->data.nFileSizeLow = st.st_size & 0xFFFFFFFF;
535 entry->data.nFileSizeHigh = st.st_size >> 32;
537 memset(&entry->data.ftCreationTime, 0, sizeof(FILETIME));
538 time_to_filetime(st.st_atime, &entry->data.ftLastAccessTime);
539 time_to_filetime(st.st_mtime, &entry->data.ftLastWriteTime);
541 entry->bhfi.nFileIndexLow = ent->d_ino;
542 entry->bhfi.nFileIndexHigh = 0;
544 entry->bhfi.nNumberOfLinks = st.st_nlink;
546 entry->bhfi_valid = TRUE;
547 } else {
548 entry->data.nFileSizeLow = 0;
549 entry->data.nFileSizeHigh = 0;
550 entry->bhfi_valid = FALSE;
553 entry->down = NULL;
554 entry->up = dir;
555 entry->expanded = FALSE;
556 entry->scanned = FALSE;
557 entry->level = level;
559 last = entry;
562 if (last)
563 last->next = NULL;
565 closedir(pdir);
568 dir->down = first_entry;
569 dir->scanned = TRUE;
572 static Entry* find_entry_unix(Entry* dir, LPCWSTR name)
574 Entry* entry;
576 for(entry=dir->down; entry; entry=entry->next) {
577 LPCWSTR p = name;
578 LPCWSTR q = entry->data.cFileName;
580 do {
581 if (!*p || *p == '/')
582 return entry;
583 } while(*p++ == *q++);
586 return 0;
589 static Entry* read_tree_unix(Root* root, LPCWSTR path, SORT_ORDER sortOrder, HWND hwnd)
591 WCHAR buffer[MAX_PATH];
592 Entry* entry = &root->entry;
593 LPCWSTR s = path;
594 PWSTR d = buffer;
596 HCURSOR old_cursor = SetCursor(LoadCursorW(0, (LPCWSTR)IDC_WAIT));
598 entry->etype = ET_UNIX;
600 while(entry) {
601 while(*s && *s != '/')
602 *d++ = *s++;
604 while(*s == '/')
605 s++;
607 *d++ = '/';
608 *d = '\0';
610 read_directory(entry, buffer, sortOrder, hwnd);
612 if (entry->down)
613 entry->expanded = TRUE;
615 if (!*s)
616 break;
618 entry = find_entry_unix(entry, s);
621 SetCursor(old_cursor);
623 return entry;
626 #endif /* __WINE__ */
628 static void free_strret(STRRET* str)
630 if (str->uType == STRRET_WSTR)
631 IMalloc_Free(Globals.iMalloc, str->UNION_MEMBER(pOleStr));
634 static LPWSTR wcscpyn(LPWSTR dest, LPCWSTR source, size_t count)
636 LPCWSTR s;
637 LPWSTR d = dest;
639 for(s=source; count&&(*d++=*s++); )
640 count--;
642 return dest;
645 static void get_strretW(STRRET* str, const SHITEMID* shiid, LPWSTR buffer, int len)
647 switch(str->uType) {
648 case STRRET_WSTR:
649 wcscpyn(buffer, str->UNION_MEMBER(pOleStr), len);
650 break;
652 case STRRET_OFFSET:
653 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)shiid+str->UNION_MEMBER(uOffset), -1, buffer, len);
654 break;
656 case STRRET_CSTR:
657 MultiByteToWideChar(CP_ACP, 0, str->UNION_MEMBER(cStr), -1, buffer, len);
662 static HRESULT name_from_pidl(IShellFolder* folder, LPITEMIDLIST pidl, LPWSTR buffer, int len, SHGDNF flags)
664 STRRET str;
666 HRESULT hr = IShellFolder_GetDisplayNameOf(folder, pidl, flags, &str);
668 if (SUCCEEDED(hr)) {
669 get_strretW(&str, &pidl->mkid, buffer, len);
670 free_strret(&str);
671 } else
672 buffer[0] = '\0';
674 return hr;
678 static HRESULT path_from_pidlW(IShellFolder* folder, LPITEMIDLIST pidl, LPWSTR buffer, int len)
680 STRRET str;
682 /* SHGDN_FORPARSING: get full path of id list */
683 HRESULT hr = IShellFolder_GetDisplayNameOf(folder, pidl, SHGDN_FORPARSING, &str);
685 if (SUCCEEDED(hr)) {
686 get_strretW(&str, &pidl->mkid, buffer, len);
687 free_strret(&str);
688 } else
689 buffer[0] = '\0';
691 return hr;
695 /* create an item id list from a file system path */
697 static LPITEMIDLIST get_path_pidl(LPWSTR path, HWND hwnd)
699 LPITEMIDLIST pidl;
700 HRESULT hr;
701 ULONG len;
702 LPWSTR buffer = path;
704 hr = IShellFolder_ParseDisplayName(Globals.iDesktop, hwnd, NULL, buffer, &len, &pidl, NULL);
705 if (FAILED(hr))
706 return NULL;
708 return pidl;
712 /* convert an item id list from relative to absolute (=relative to the desktop) format */
714 static LPITEMIDLIST get_to_absolute_pidl(Entry* entry, HWND hwnd)
716 if (entry->up && entry->up->etype==ET_SHELL) {
717 LPITEMIDLIST idl = NULL;
719 while (entry->up) {
720 idl = ILCombine(ILClone(entry->pidl), idl);
721 entry = entry->up;
724 return idl;
725 } else if (entry->etype == ET_WINDOWS) {
726 WCHAR path[MAX_PATH];
728 get_path(entry, path);
730 return get_path_pidl(path, hwnd);
731 } else if (entry->pidl)
732 return ILClone(entry->pidl);
734 return NULL;
738 static HICON extract_icon(IShellFolder* folder, LPCITEMIDLIST pidl)
740 IExtractIconW* pExtract;
742 if (SUCCEEDED(IShellFolder_GetUIObjectOf(folder, 0, 1, (LPCITEMIDLIST*)&pidl, &IID_IExtractIconW, 0, (LPVOID*)&pExtract))) {
743 WCHAR path[_MAX_PATH];
744 unsigned flags;
745 HICON hicon;
746 int idx;
748 if (SUCCEEDED(IExtractIconW_GetIconLocation(pExtract, GIL_FORSHELL, path, _MAX_PATH, &idx, &flags))) {
749 if (!(flags & GIL_NOTFILENAME)) {
750 if (idx == -1)
751 idx = 0; /* special case for some control panel applications */
753 if ((int)ExtractIconExW(path, idx, 0, &hicon, 1) > 0)
754 flags &= ~GIL_DONTCACHE;
755 } else {
756 HICON hIconLarge = 0;
758 HRESULT hr = IExtractIconW_Extract(pExtract, path, idx, &hIconLarge, &hicon, MAKELONG(0/*GetSystemMetrics(SM_CXICON)*/,GetSystemMetrics(SM_CXSMICON)));
760 if (SUCCEEDED(hr))
761 DestroyIcon(hIconLarge);
764 return hicon;
768 return 0;
772 static Entry* find_entry_shell(Entry* dir, LPCITEMIDLIST pidl)
774 Entry* entry;
776 for(entry=dir->down; entry; entry=entry->next) {
777 if (entry->pidl->mkid.cb == pidl->mkid.cb &&
778 !memcmp(entry->pidl, pidl, entry->pidl->mkid.cb))
779 return entry;
782 return 0;
785 static Entry* read_tree_shell(Root* root, LPITEMIDLIST pidl, SORT_ORDER sortOrder, HWND hwnd)
787 Entry* entry = &root->entry;
788 Entry* next;
789 LPITEMIDLIST next_pidl = pidl;
790 IShellFolder* folder;
791 IShellFolder* child = NULL;
792 HRESULT hr;
794 HCURSOR old_cursor = SetCursor(LoadCursorW(0, (LPCWSTR)IDC_WAIT));
796 entry->etype = ET_SHELL;
797 folder = Globals.iDesktop;
799 while(entry) {
800 entry->pidl = next_pidl;
801 entry->folder = folder;
803 if (!pidl->mkid.cb)
804 break;
806 /* copy first element of item idlist */
807 next_pidl = IMalloc_Alloc(Globals.iMalloc, pidl->mkid.cb+sizeof(USHORT));
808 memcpy(next_pidl, pidl, pidl->mkid.cb);
809 ((LPITEMIDLIST)((LPBYTE)next_pidl+pidl->mkid.cb))->mkid.cb = 0;
811 hr = IShellFolder_BindToObject(folder, next_pidl, 0, &IID_IShellFolder, (void**)&child);
812 if (FAILED(hr))
813 break;
815 read_directory(entry, NULL, sortOrder, hwnd);
817 if (entry->down)
818 entry->expanded = TRUE;
820 next = find_entry_shell(entry, next_pidl);
821 if (!next)
822 break;
824 folder = child;
825 entry = next;
827 /* go to next element */
828 pidl = (LPITEMIDLIST) ((LPBYTE)pidl+pidl->mkid.cb);
831 SetCursor(old_cursor);
833 return entry;
837 static void fill_w32fdata_shell(IShellFolder* folder, LPCITEMIDLIST pidl, SFGAOF attribs, WIN32_FIND_DATAW* w32fdata)
839 if (!(attribs & SFGAO_FILESYSTEM) ||
840 FAILED(SHGetDataFromIDListW(folder, pidl, SHGDFIL_FINDDATA, w32fdata, sizeof(WIN32_FIND_DATAW)))) {
841 WIN32_FILE_ATTRIBUTE_DATA fad;
842 IDataObject* pDataObj;
844 STGMEDIUM medium = {0, {0}, 0};
845 FORMATETC fmt = {Globals.cfStrFName, 0, DVASPECT_CONTENT, -1, TYMED_HGLOBAL};
847 HRESULT hr = IShellFolder_GetUIObjectOf(folder, 0, 1, &pidl, &IID_IDataObject, 0, (LPVOID*)&pDataObj);
849 if (SUCCEEDED(hr)) {
850 hr = IDataObject_GetData(pDataObj, &fmt, &medium);
852 IDataObject_Release(pDataObj);
854 if (SUCCEEDED(hr)) {
855 LPCWSTR path = GlobalLock(medium.UNION_MEMBER(hGlobal));
856 UINT sem_org = SetErrorMode(SEM_FAILCRITICALERRORS);
858 if (GetFileAttributesExW(path, GetFileExInfoStandard, &fad)) {
859 w32fdata->dwFileAttributes = fad.dwFileAttributes;
860 w32fdata->ftCreationTime = fad.ftCreationTime;
861 w32fdata->ftLastAccessTime = fad.ftLastAccessTime;
862 w32fdata->ftLastWriteTime = fad.ftLastWriteTime;
864 if (!(fad.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
865 w32fdata->nFileSizeLow = fad.nFileSizeLow;
866 w32fdata->nFileSizeHigh = fad.nFileSizeHigh;
870 SetErrorMode(sem_org);
872 GlobalUnlock(medium.UNION_MEMBER(hGlobal));
873 GlobalFree(medium.UNION_MEMBER(hGlobal));
878 if (attribs & (SFGAO_FOLDER|SFGAO_HASSUBFOLDER))
879 w32fdata->dwFileAttributes |= FILE_ATTRIBUTE_DIRECTORY;
881 if (attribs & SFGAO_READONLY)
882 w32fdata->dwFileAttributes |= FILE_ATTRIBUTE_READONLY;
884 if (attribs & SFGAO_COMPRESSED)
885 w32fdata->dwFileAttributes |= FILE_ATTRIBUTE_COMPRESSED;
889 static void read_directory_shell(Entry* dir, HWND hwnd)
891 IShellFolder* folder = dir->folder;
892 int level = dir->level + 1;
893 HRESULT hr;
895 IShellFolder* child;
896 IEnumIDList* idlist;
898 Entry* first_entry = NULL;
899 Entry* last = NULL;
900 Entry* entry;
902 if (!folder)
903 return;
905 hr = IShellFolder_EnumObjects(folder, hwnd, SHCONTF_FOLDERS|SHCONTF_NONFOLDERS|SHCONTF_INCLUDEHIDDEN|SHCONTF_SHAREABLE|SHCONTF_STORAGE, &idlist);
907 if (SUCCEEDED(hr)) {
908 for(;;) {
909 #define FETCH_ITEM_COUNT 32
910 LPITEMIDLIST pidls[FETCH_ITEM_COUNT];
911 SFGAOF attribs;
912 ULONG cnt = 0;
913 ULONG n;
915 memset(pidls, 0, sizeof(pidls));
917 hr = IEnumIDList_Next(idlist, FETCH_ITEM_COUNT, pidls, &cnt);
918 if (FAILED(hr))
919 break;
921 if (hr == S_FALSE)
922 break;
924 for(n=0; n<cnt; ++n) {
925 entry = alloc_entry();
927 if (!first_entry)
928 first_entry = entry;
930 if (last)
931 last->next = entry;
933 memset(&entry->data, 0, sizeof(WIN32_FIND_DATAW));
934 entry->bhfi_valid = FALSE;
936 attribs = ~SFGAO_FILESYSTEM; /*SFGAO_HASSUBFOLDER|SFGAO_FOLDER; SFGAO_FILESYSTEM sorgt dafür, daß "My Documents" anstatt von "Martin's Documents" angezeigt wird */
938 hr = IShellFolder_GetAttributesOf(folder, 1, (LPCITEMIDLIST*)&pidls[n], &attribs);
940 if (SUCCEEDED(hr)) {
941 if (attribs != (SFGAOF)~SFGAO_FILESYSTEM) {
942 fill_w32fdata_shell(folder, pidls[n], attribs, &entry->data);
944 entry->bhfi_valid = TRUE;
945 } else
946 attribs = 0;
947 } else
948 attribs = 0;
950 entry->pidl = pidls[n];
952 if (entry->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
953 hr = IShellFolder_BindToObject(folder, pidls[n], 0, &IID_IShellFolder, (void**)&child);
955 if (SUCCEEDED(hr))
956 entry->folder = child;
957 else
958 entry->folder = NULL;
960 else
961 entry->folder = NULL;
963 if (!entry->data.cFileName[0])
964 /*hr = */name_from_pidl(folder, pidls[n], entry->data.cFileName, MAX_PATH, /*SHGDN_INFOLDER*/0x2000/*0x2000=SHGDN_INCLUDE_NONFILESYS*/);
966 /* get display icons for files and virtual objects */
967 if (!(entry->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
968 !(attribs & SFGAO_FILESYSTEM)) {
969 entry->hicon = extract_icon(folder, pidls[n]);
971 if (!entry->hicon)
972 entry->hicon = (HICON)-1; /* don't try again later */
975 entry->down = NULL;
976 entry->up = dir;
977 entry->expanded = FALSE;
978 entry->scanned = FALSE;
979 entry->level = level;
981 entry->etype = ET_SHELL;
982 entry->bhfi_valid = FALSE;
984 last = entry;
988 IEnumIDList_Release(idlist);
991 if (last)
992 last->next = NULL;
994 dir->down = first_entry;
995 dir->scanned = TRUE;
998 /* sort order for different directory/file types */
999 enum TYPE_ORDER {
1000 TO_DIR = 0,
1001 TO_DOT = 1,
1002 TO_DOTDOT = 2,
1003 TO_OTHER_DIR = 3,
1004 TO_FILE = 4
1007 /* distinguish between ".", ".." and any other directory names */
1008 static int TypeOrderFromDirname(LPCWSTR name)
1010 if (name[0] == '.') {
1011 if (name[1] == '\0')
1012 return TO_DOT; /* "." */
1014 if (name[1]=='.' && name[2]=='\0')
1015 return TO_DOTDOT; /* ".." */
1018 return TO_OTHER_DIR; /* anything else */
1021 /* directories first... */
1022 static int compareType(const WIN32_FIND_DATAW* fd1, const WIN32_FIND_DATAW* fd2)
1024 int order1 = fd1->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY? TO_DIR: TO_FILE;
1025 int order2 = fd2->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY? TO_DIR: TO_FILE;
1027 /* Handle "." and ".." as special case and move them at the very first beginning. */
1028 if (order1==TO_DIR && order2==TO_DIR) {
1029 order1 = TypeOrderFromDirname(fd1->cFileName);
1030 order2 = TypeOrderFromDirname(fd2->cFileName);
1033 return order2==order1? 0: order1<order2? -1: 1;
1037 static int compareName(const void* arg1, const void* arg2)
1039 const WIN32_FIND_DATAW* fd1 = &(*(const Entry* const*)arg1)->data;
1040 const WIN32_FIND_DATAW* fd2 = &(*(const Entry* const*)arg2)->data;
1042 int cmp = compareType(fd1, fd2);
1043 if (cmp)
1044 return cmp;
1046 return lstrcmpiW(fd1->cFileName, fd2->cFileName);
1049 static int compareExt(const void* arg1, const void* arg2)
1051 const WIN32_FIND_DATAW* fd1 = &(*(const Entry* const*)arg1)->data;
1052 const WIN32_FIND_DATAW* fd2 = &(*(const Entry* const*)arg2)->data;
1053 const WCHAR *name1, *name2, *ext1, *ext2;
1055 int cmp = compareType(fd1, fd2);
1056 if (cmp)
1057 return cmp;
1059 name1 = fd1->cFileName;
1060 name2 = fd2->cFileName;
1062 ext1 = strrchrW(name1, '.');
1063 ext2 = strrchrW(name2, '.');
1065 if (ext1)
1066 ext1++;
1067 else
1068 ext1 = sEmpty;
1070 if (ext2)
1071 ext2++;
1072 else
1073 ext2 = sEmpty;
1075 cmp = lstrcmpiW(ext1, ext2);
1076 if (cmp)
1077 return cmp;
1079 return lstrcmpiW(name1, name2);
1082 static int compareSize(const void* arg1, const void* arg2)
1084 const WIN32_FIND_DATAW* fd1 = &(*(const Entry* const*)arg1)->data;
1085 const WIN32_FIND_DATAW* fd2 = &(*(const Entry* const*)arg2)->data;
1087 int cmp = compareType(fd1, fd2);
1088 if (cmp)
1089 return cmp;
1091 cmp = fd2->nFileSizeHigh - fd1->nFileSizeHigh;
1093 if (cmp < 0)
1094 return -1;
1095 else if (cmp > 0)
1096 return 1;
1098 cmp = fd2->nFileSizeLow - fd1->nFileSizeLow;
1100 return cmp<0? -1: cmp>0? 1: 0;
1103 static int compareDate(const void* arg1, const void* arg2)
1105 const WIN32_FIND_DATAW* fd1 = &(*(const Entry* const*)arg1)->data;
1106 const WIN32_FIND_DATAW* fd2 = &(*(const Entry* const*)arg2)->data;
1108 int cmp = compareType(fd1, fd2);
1109 if (cmp)
1110 return cmp;
1112 return CompareFileTime(&fd2->ftLastWriteTime, &fd1->ftLastWriteTime);
1116 static int (*sortFunctions[])(const void* arg1, const void* arg2) = {
1117 compareName, /* SORT_NAME */
1118 compareExt, /* SORT_EXT */
1119 compareSize, /* SORT_SIZE */
1120 compareDate /* SORT_DATE */
1124 static void SortDirectory(Entry* dir, SORT_ORDER sortOrder)
1126 Entry* entry;
1127 Entry** array, **p;
1128 int len;
1130 len = 0;
1131 for(entry=dir->down; entry; entry=entry->next)
1132 len++;
1134 if (len) {
1135 array = HeapAlloc(GetProcessHeap(), 0, len*sizeof(Entry*));
1137 p = array;
1138 for(entry=dir->down; entry; entry=entry->next)
1139 *p++ = entry;
1141 /* call qsort with the appropriate compare function */
1142 qsort(array, len, sizeof(array[0]), sortFunctions[sortOrder]);
1144 dir->down = array[0];
1146 for(p=array; --len; p++)
1147 p[0]->next = p[1];
1149 (*p)->next = 0;
1151 HeapFree(GetProcessHeap(), 0, array);
1156 static void read_directory(Entry* dir, LPCWSTR path, SORT_ORDER sortOrder, HWND hwnd)
1158 WCHAR buffer[MAX_PATH];
1159 Entry* entry;
1160 LPCWSTR s;
1161 PWSTR d;
1163 if (dir->etype == ET_SHELL)
1165 read_directory_shell(dir, hwnd);
1167 if (Globals.prescan_node) {
1168 s = path;
1169 d = buffer;
1171 while(*s)
1172 *d++ = *s++;
1174 *d++ = '\\';
1176 for(entry=dir->down; entry; entry=entry->next)
1177 if (entry->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
1178 read_directory_shell(entry, hwnd);
1179 SortDirectory(entry, sortOrder);
1183 else
1184 #ifdef __WINE__
1185 if (dir->etype == ET_UNIX)
1187 read_directory_unix(dir, path);
1189 if (Globals.prescan_node) {
1190 s = path;
1191 d = buffer;
1193 while(*s)
1194 *d++ = *s++;
1196 *d++ = '/';
1198 for(entry=dir->down; entry; entry=entry->next)
1199 if (entry->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
1200 lstrcpyW(d, entry->data.cFileName);
1201 read_directory_unix(entry, buffer);
1202 SortDirectory(entry, sortOrder);
1206 else
1207 #endif
1209 read_directory_win(dir, path);
1211 if (Globals.prescan_node) {
1212 s = path;
1213 d = buffer;
1215 while(*s)
1216 *d++ = *s++;
1218 *d++ = '\\';
1220 for(entry=dir->down; entry; entry=entry->next)
1221 if (entry->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
1222 lstrcpyW(d, entry->data.cFileName);
1223 read_directory_win(entry, buffer);
1224 SortDirectory(entry, sortOrder);
1229 SortDirectory(dir, sortOrder);
1233 static Entry* read_tree(Root* root, LPCWSTR path, LPITEMIDLIST pidl, LPWSTR drv, SORT_ORDER sortOrder, HWND hwnd)
1235 #ifdef __WINE__
1236 static const WCHAR sSlash[] = {'/', '\0'};
1237 #endif
1238 static const WCHAR sBackslash[] = {'\\', '\0'};
1240 if (pidl)
1242 /* read shell namespace tree */
1243 root->drive_type = DRIVE_UNKNOWN;
1244 drv[0] = '\\';
1245 drv[1] = '\0';
1246 load_string(root->volname, sizeof(root->volname)/sizeof(root->volname[0]), IDS_DESKTOP);
1247 root->fs_flags = 0;
1248 load_string(root->fs, sizeof(root->fs)/sizeof(root->fs[0]), IDS_SHELL);
1250 return read_tree_shell(root, pidl, sortOrder, hwnd);
1252 else
1253 #ifdef __WINE__
1254 if (*path == '/')
1256 /* read unix file system tree */
1257 root->drive_type = GetDriveTypeW(path);
1259 lstrcatW(drv, sSlash);
1260 load_string(root->volname, sizeof(root->volname)/sizeof(root->volname[0]), IDS_ROOT_FS);
1261 root->fs_flags = 0;
1262 load_string(root->fs, sizeof(root->fs)/sizeof(root->fs[0]), IDS_UNIXFS);
1264 lstrcpyW(root->path, sSlash);
1266 return read_tree_unix(root, path, sortOrder, hwnd);
1268 #endif
1270 /* read WIN32 file system tree */
1271 root->drive_type = GetDriveTypeW(path);
1273 lstrcatW(drv, sBackslash);
1274 GetVolumeInformationW(drv, root->volname, _MAX_FNAME, 0, 0, &root->fs_flags, root->fs, _MAX_DIR);
1276 lstrcpyW(root->path, drv);
1278 return read_tree_win(root, path, sortOrder, hwnd);
1282 /* flags to filter different file types */
1283 enum TYPE_FILTER {
1284 TF_DIRECTORIES = 0x01,
1285 TF_PROGRAMS = 0x02,
1286 TF_DOCUMENTS = 0x04,
1287 TF_OTHERS = 0x08,
1288 TF_HIDDEN = 0x10,
1289 TF_ALL = 0x1F
1293 static ChildWnd* alloc_child_window(LPCWSTR path, LPITEMIDLIST pidl, HWND hwnd)
1295 WCHAR drv[_MAX_DRIVE+1], dir[_MAX_DIR], name[_MAX_FNAME], ext[_MAX_EXT];
1296 WCHAR dir_path[MAX_PATH];
1297 static const WCHAR sAsterics[] = {'*', '\0'};
1298 static const WCHAR sTitleFmt[] = {'%','s',' ','-',' ','%','s','\0'};
1300 ChildWnd* child = HeapAlloc(GetProcessHeap(), 0, sizeof(ChildWnd));
1301 Root* root = &child->root;
1302 Entry* entry;
1304 memset(child, 0, sizeof(ChildWnd));
1306 child->left.treePane = TRUE;
1307 child->left.visible_cols = 0;
1309 child->right.treePane = FALSE;
1310 child->right.visible_cols = COL_SIZE|COL_DATE|COL_TIME|COL_ATTRIBUTES|COL_INDEX|COL_LINKS;
1312 child->pos.length = sizeof(WINDOWPLACEMENT);
1313 child->pos.flags = 0;
1314 child->pos.showCmd = SW_SHOWNORMAL;
1315 child->pos.rcNormalPosition.left = CW_USEDEFAULT;
1316 child->pos.rcNormalPosition.top = CW_USEDEFAULT;
1317 child->pos.rcNormalPosition.right = CW_USEDEFAULT;
1318 child->pos.rcNormalPosition.bottom = CW_USEDEFAULT;
1320 child->focus_pane = 0;
1321 child->split_pos = DEFAULT_SPLIT_POS;
1322 child->sortOrder = SORT_NAME;
1323 child->header_wdths_ok = FALSE;
1325 if (path)
1327 int pathlen = strlenW(path);
1328 const WCHAR *npath = path;
1330 if (path[0] == '"' && path[pathlen - 1] == '"')
1332 npath++;
1333 pathlen--;
1335 lstrcpynW(child->path, npath, pathlen + 1);
1337 _wsplitpath(child->path, drv, dir, name, ext);
1340 lstrcpyW(child->filter_pattern, sAsterics);
1341 child->filter_flags = TF_ALL;
1343 root->entry.level = 0;
1345 lstrcpyW(dir_path, drv);
1346 lstrcatW(dir_path, dir);
1347 entry = read_tree(root, dir_path, pidl, drv, child->sortOrder, hwnd);
1349 if (root->entry.etype == ET_SHELL)
1350 load_string(root->entry.data.cFileName, sizeof(root->entry.data.cFileName)/sizeof(root->entry.data.cFileName[0]), IDS_DESKTOP);
1351 else
1352 wsprintfW(root->entry.data.cFileName, sTitleFmt, drv, root->fs);
1354 root->entry.data.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1356 child->left.root = &root->entry;
1357 child->right.root = NULL;
1359 set_curdir(child, entry, 0, hwnd);
1361 return child;
1365 /* free all memory associated with a child window */
1366 static void free_child_window(ChildWnd* child)
1368 free_entries(&child->root.entry);
1369 HeapFree(GetProcessHeap(), 0, child);
1373 /* get full path of specified directory entry */
1374 static void get_path(Entry* dir, PWSTR path)
1376 Entry* entry;
1377 int len = 0;
1378 int level = 0;
1380 if (dir->etype == ET_SHELL)
1382 SFGAOF attribs;
1383 HRESULT hr = S_OK;
1385 path[0] = '\0';
1387 attribs = 0;
1389 if (dir->folder)
1390 hr = IShellFolder_GetAttributesOf(dir->folder, 1, (LPCITEMIDLIST*)&dir->pidl, &attribs);
1392 if (SUCCEEDED(hr) && (attribs&SFGAO_FILESYSTEM)) {
1393 IShellFolder* parent = dir->up? dir->up->folder: Globals.iDesktop;
1395 hr = path_from_pidlW(parent, dir->pidl, path, MAX_PATH);
1398 else
1400 for(entry=dir; entry; level++) {
1401 LPCWSTR name;
1402 int l;
1405 LPCWSTR s;
1406 name = entry->data.cFileName;
1407 s = name;
1409 for(l=0; *s && *s != '/' && *s != '\\'; s++)
1410 l++;
1413 if (entry->up) {
1414 if (l > 0) {
1415 memmove(path+l+1, path, len*sizeof(WCHAR));
1416 memcpy(path+1, name, l*sizeof(WCHAR));
1417 len += l+1;
1419 if (entry->etype == ET_UNIX)
1420 path[0] = '/';
1421 else
1422 path[0] = '\\';
1425 entry = entry->up;
1426 } else {
1427 memmove(path+l, path, len*sizeof(WCHAR));
1428 memcpy(path, name, l*sizeof(WCHAR));
1429 len += l;
1430 break;
1434 if (!level) {
1435 if (entry->etype == ET_UNIX)
1436 path[len++] = '/';
1437 else
1438 path[len++] = '\\';
1441 path[len] = '\0';
1445 static windowOptions load_registry_settings(void)
1447 DWORD size;
1448 DWORD type;
1449 HKEY hKey;
1450 windowOptions opts;
1451 LOGFONTW logfont;
1453 RegOpenKeyExW( HKEY_CURRENT_USER, registry_key,
1454 0, KEY_QUERY_VALUE, &hKey );
1456 size = sizeof(DWORD);
1458 if( RegQueryValueExW( hKey, reg_start_x, NULL, &type,
1459 (LPBYTE) &opts.start_x, &size ) != ERROR_SUCCESS )
1460 opts.start_x = CW_USEDEFAULT;
1462 if( RegQueryValueExW( hKey, reg_start_y, NULL, &type,
1463 (LPBYTE) &opts.start_y, &size ) != ERROR_SUCCESS )
1464 opts.start_y = CW_USEDEFAULT;
1466 if( RegQueryValueExW( hKey, reg_width, NULL, &type,
1467 (LPBYTE) &opts.width, &size ) != ERROR_SUCCESS )
1468 opts.width = CW_USEDEFAULT;
1470 if( RegQueryValueExW( hKey, reg_height, NULL, &type,
1471 (LPBYTE) &opts.height, &size ) != ERROR_SUCCESS )
1472 opts.height = CW_USEDEFAULT;
1473 size=sizeof(logfont);
1474 if( RegQueryValueExW( hKey, reg_logfont, NULL, &type,
1475 (LPBYTE) &logfont, &size ) != ERROR_SUCCESS )
1476 GetObjectW(GetStockObject(DEFAULT_GUI_FONT),sizeof(logfont),&logfont);
1478 RegCloseKey( hKey );
1480 Globals.hfont = CreateFontIndirectW(&logfont);
1481 return opts;
1484 static void save_registry_settings(void)
1486 WINDOWINFO wi;
1487 HKEY hKey;
1488 INT width, height;
1489 LOGFONTW logfont;
1491 wi.cbSize = sizeof( WINDOWINFO );
1492 GetWindowInfo(Globals.hMainWnd, &wi);
1493 width = wi.rcWindow.right - wi.rcWindow.left;
1494 height = wi.rcWindow.bottom - wi.rcWindow.top;
1496 if ( RegOpenKeyExW( HKEY_CURRENT_USER, registry_key,
1497 0, KEY_SET_VALUE, &hKey ) != ERROR_SUCCESS )
1499 /* Unable to save registry settings - try to create key */
1500 if ( RegCreateKeyExW( HKEY_CURRENT_USER, registry_key,
1501 0, NULL, REG_OPTION_NON_VOLATILE,
1502 KEY_SET_VALUE, NULL, &hKey, NULL ) != ERROR_SUCCESS )
1504 /* FIXME: Cannot create key */
1505 return;
1508 /* Save all of the settings */
1509 RegSetValueExW( hKey, reg_start_x, 0, REG_DWORD,
1510 (LPBYTE) &wi.rcWindow.left, sizeof(DWORD) );
1511 RegSetValueExW( hKey, reg_start_y, 0, REG_DWORD,
1512 (LPBYTE) &wi.rcWindow.top, sizeof(DWORD) );
1513 RegSetValueExW( hKey, reg_width, 0, REG_DWORD,
1514 (LPBYTE) &width, sizeof(DWORD) );
1515 RegSetValueExW( hKey, reg_height, 0, REG_DWORD,
1516 (LPBYTE) &height, sizeof(DWORD) );
1517 GetObjectW(Globals.hfont, sizeof(logfont), &logfont);
1518 RegSetValueExW( hKey, reg_logfont, 0, REG_BINARY,
1519 (LPBYTE)&logfont, sizeof(LOGFONTW) );
1521 /* TODO: Save more settings here (List vs. Detailed View, etc.) */
1522 RegCloseKey( hKey );
1525 static void resize_frame_rect(HWND hwnd, PRECT prect)
1527 int new_top;
1528 RECT rt;
1530 if (IsWindowVisible(Globals.htoolbar)) {
1531 SendMessageW(Globals.htoolbar, WM_SIZE, 0, 0);
1532 GetClientRect(Globals.htoolbar, &rt);
1533 prect->top = rt.bottom+3;
1534 prect->bottom -= rt.bottom+3;
1537 if (IsWindowVisible(Globals.hdrivebar)) {
1538 SendMessageW(Globals.hdrivebar, WM_SIZE, 0, 0);
1539 GetClientRect(Globals.hdrivebar, &rt);
1540 new_top = --prect->top + rt.bottom+3;
1541 MoveWindow(Globals.hdrivebar, 0, prect->top, rt.right, new_top, TRUE);
1542 prect->top = new_top;
1543 prect->bottom -= rt.bottom+2;
1546 if (IsWindowVisible(Globals.hstatusbar)) {
1547 int parts[] = {300, 500};
1549 SendMessageW(Globals.hstatusbar, WM_SIZE, 0, 0);
1550 SendMessageW(Globals.hstatusbar, SB_SETPARTS, 2, (LPARAM)&parts);
1551 GetClientRect(Globals.hstatusbar, &rt);
1552 prect->bottom -= rt.bottom;
1555 MoveWindow(Globals.hmdiclient, prect->left-1,prect->top-1,prect->right+2,prect->bottom+1, TRUE);
1558 static void resize_frame(HWND hwnd, int cx, int cy)
1560 RECT rect;
1562 rect.left = 0;
1563 rect.top = 0;
1564 rect.right = cx;
1565 rect.bottom = cy;
1567 resize_frame_rect(hwnd, &rect);
1570 static void resize_frame_client(HWND hwnd)
1572 RECT rect;
1574 GetClientRect(hwnd, &rect);
1576 resize_frame_rect(hwnd, &rect);
1580 static HHOOK hcbthook;
1581 static ChildWnd* newchild = NULL;
1583 static LRESULT CALLBACK CBTProc(int code, WPARAM wparam, LPARAM lparam)
1585 if (code==HCBT_CREATEWND && newchild) {
1586 ChildWnd* child = newchild;
1587 newchild = NULL;
1589 child->hwnd = (HWND) wparam;
1590 SetWindowLongPtrW(child->hwnd, GWLP_USERDATA, (LPARAM)child);
1593 return CallNextHookEx(hcbthook, code, wparam, lparam);
1596 static HWND create_child_window(ChildWnd* child)
1598 MDICREATESTRUCTW mcs;
1599 int idx;
1601 mcs.szClass = sWINEFILETREE;
1602 mcs.szTitle = child->path;
1603 mcs.hOwner = Globals.hInstance;
1604 mcs.x = child->pos.rcNormalPosition.left;
1605 mcs.y = child->pos.rcNormalPosition.top;
1606 mcs.cx = child->pos.rcNormalPosition.right-child->pos.rcNormalPosition.left;
1607 mcs.cy = child->pos.rcNormalPosition.bottom-child->pos.rcNormalPosition.top;
1608 mcs.style = 0;
1609 mcs.lParam = 0;
1611 hcbthook = SetWindowsHookExW(WH_CBT, CBTProc, 0, GetCurrentThreadId());
1613 newchild = child;
1614 child->hwnd = (HWND)SendMessageW(Globals.hmdiclient, WM_MDICREATE, 0, (LPARAM)&mcs);
1615 if (!child->hwnd) {
1616 UnhookWindowsHookEx(hcbthook);
1617 return 0;
1620 UnhookWindowsHookEx(hcbthook);
1622 SendMessageW(child->left.hwnd, LB_SETITEMHEIGHT, 1, max(Globals.spaceSize.cy,IMAGE_HEIGHT+3));
1623 SendMessageW(child->right.hwnd, LB_SETITEMHEIGHT, 1, max(Globals.spaceSize.cy,IMAGE_HEIGHT+3));
1625 idx = SendMessageW(child->left.hwnd, LB_FINDSTRING, 0, (LPARAM)child->left.cur);
1626 SendMessageW(child->left.hwnd, LB_SETCURSEL, idx, 0);
1628 return child->hwnd;
1631 #define RFF_NODEFAULT 0x02 /* No default item selected. */
1633 static void WineFile_OnRun( HWND hwnd )
1635 static const WCHAR shell32_dll[] = {'S','H','E','L','L','3','2','.','D','L','L',0};
1636 void (WINAPI *pRunFileDlgAW )(HWND, HICON, LPWSTR, LPWSTR, LPWSTR, DWORD);
1637 HMODULE hshell = GetModuleHandleW( shell32_dll );
1638 HICON hIcon = LoadIconW(Globals.hInstance, MAKEINTRESOURCEW(IDI_WINEFILE));
1640 pRunFileDlgAW = (void*)GetProcAddress(hshell, (LPCSTR)61);
1641 if (pRunFileDlgAW) pRunFileDlgAW( hwnd, hIcon, NULL, NULL, NULL, RFF_NODEFAULT);
1644 static INT_PTR CALLBACK DestinationDlgProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam)
1646 WCHAR b1[BUFFER_LEN], b2[BUFFER_LEN];
1648 switch(nmsg) {
1649 case WM_INITDIALOG:
1650 SetWindowLongPtrW(hwnd, GWLP_USERDATA, lparam);
1651 SetWindowTextW(GetDlgItem(hwnd, 201), (LPCWSTR)lparam);
1652 return 1;
1654 case WM_COMMAND: {
1655 int id = (int)wparam;
1657 switch(id) {
1658 case IDOK: {
1659 LPWSTR dest = (LPWSTR)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
1660 GetWindowTextW(GetDlgItem(hwnd, 201), dest, MAX_PATH);
1661 EndDialog(hwnd, id);
1662 break;}
1664 case IDCANCEL:
1665 EndDialog(hwnd, id);
1666 break;
1668 case 254:
1669 MessageBoxW(hwnd, RS(b1,IDS_NO_IMPL), RS(b2,IDS_WINEFILE), MB_OK);
1670 break;
1673 return 1;
1677 return 0;
1681 struct FilterDialog {
1682 WCHAR pattern[MAX_PATH];
1683 int flags;
1686 static INT_PTR CALLBACK FilterDialogDlgProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam)
1688 static struct FilterDialog* dlg;
1690 switch(nmsg) {
1691 case WM_INITDIALOG:
1692 dlg = (struct FilterDialog*) lparam;
1693 SetWindowTextW(GetDlgItem(hwnd, IDC_VIEW_PATTERN), dlg->pattern);
1694 set_check(hwnd, IDC_VIEW_TYPE_DIRECTORIES, dlg->flags&TF_DIRECTORIES);
1695 set_check(hwnd, IDC_VIEW_TYPE_PROGRAMS, dlg->flags&TF_PROGRAMS);
1696 set_check(hwnd, IDC_VIEW_TYPE_DOCUMENTS, dlg->flags&TF_DOCUMENTS);
1697 set_check(hwnd, IDC_VIEW_TYPE_OTHERS, dlg->flags&TF_OTHERS);
1698 set_check(hwnd, IDC_VIEW_TYPE_HIDDEN, dlg->flags&TF_HIDDEN);
1699 return 1;
1701 case WM_COMMAND: {
1702 int id = (int)wparam;
1704 if (id == IDOK) {
1705 int flags = 0;
1707 GetWindowTextW(GetDlgItem(hwnd, IDC_VIEW_PATTERN), dlg->pattern, MAX_PATH);
1709 flags |= get_check(hwnd, IDC_VIEW_TYPE_DIRECTORIES) ? TF_DIRECTORIES : 0;
1710 flags |= get_check(hwnd, IDC_VIEW_TYPE_PROGRAMS) ? TF_PROGRAMS : 0;
1711 flags |= get_check(hwnd, IDC_VIEW_TYPE_DOCUMENTS) ? TF_DOCUMENTS : 0;
1712 flags |= get_check(hwnd, IDC_VIEW_TYPE_OTHERS) ? TF_OTHERS : 0;
1713 flags |= get_check(hwnd, IDC_VIEW_TYPE_HIDDEN) ? TF_HIDDEN : 0;
1715 dlg->flags = flags;
1717 EndDialog(hwnd, id);
1718 } else if (id == IDCANCEL)
1719 EndDialog(hwnd, id);
1721 return 1;}
1724 return 0;
1728 struct PropertiesDialog {
1729 WCHAR path[MAX_PATH];
1730 Entry entry;
1731 void* pVersionData;
1734 /* Structure used to store enumerated languages and code pages. */
1735 struct LANGANDCODEPAGE {
1736 WORD wLanguage;
1737 WORD wCodePage;
1738 } *lpTranslate;
1740 static LPCSTR InfoStrings[] = {
1741 "Comments",
1742 "CompanyName",
1743 "FileDescription",
1744 "FileVersion",
1745 "InternalName",
1746 "LegalCopyright",
1747 "LegalTrademarks",
1748 "OriginalFilename",
1749 "PrivateBuild",
1750 "ProductName",
1751 "ProductVersion",
1752 "SpecialBuild",
1753 NULL
1756 static void PropDlg_DisplayValue(HWND hlbox, HWND hedit)
1758 int idx = SendMessageW(hlbox, LB_GETCURSEL, 0, 0);
1760 if (idx != LB_ERR) {
1761 LPCWSTR pValue = (LPCWSTR)SendMessageW(hlbox, LB_GETITEMDATA, idx, 0);
1763 if (pValue)
1764 SetWindowTextW(hedit, pValue);
1768 static void CheckForFileInfo(struct PropertiesDialog* dlg, HWND hwnd, LPCWSTR strFilename)
1770 static const WCHAR sBackSlash[] = {'\\','\0'};
1771 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'};
1772 static const WCHAR sStringFileInfo[] = {'\\','S','t','r','i','n','g','F','i','l','e','I','n','f','o','\\',
1773 '%','0','4','x','%','0','4','x','\\','%','s','\0'};
1774 static const WCHAR sFmt[] = {'%','d','.','%','d','.','%','d','.','%','d','\0'};
1775 DWORD dwVersionDataLen = GetFileVersionInfoSizeW(strFilename, NULL);
1777 if (dwVersionDataLen) {
1778 dlg->pVersionData = HeapAlloc(GetProcessHeap(), 0, dwVersionDataLen);
1780 if (GetFileVersionInfoW(strFilename, 0, dwVersionDataLen, dlg->pVersionData)) {
1781 LPVOID pVal;
1782 UINT nValLen;
1784 if (VerQueryValueW(dlg->pVersionData, sBackSlash, &pVal, &nValLen)) {
1785 if (nValLen == sizeof(VS_FIXEDFILEINFO)) {
1786 VS_FIXEDFILEINFO* pFixedFileInfo = (VS_FIXEDFILEINFO*)pVal;
1787 WCHAR buffer[BUFFER_LEN];
1789 sprintfW(buffer, sFmt,
1790 HIWORD(pFixedFileInfo->dwFileVersionMS), LOWORD(pFixedFileInfo->dwFileVersionMS),
1791 HIWORD(pFixedFileInfo->dwFileVersionLS), LOWORD(pFixedFileInfo->dwFileVersionLS));
1793 SetDlgItemTextW(hwnd, IDC_STATIC_PROP_VERSION, buffer);
1797 /* Read the list of languages and code pages. */
1798 if (VerQueryValueW(dlg->pVersionData, sTranslation, &pVal, &nValLen)) {
1799 struct LANGANDCODEPAGE* pTranslate = (struct LANGANDCODEPAGE*)pVal;
1800 struct LANGANDCODEPAGE* pEnd = (struct LANGANDCODEPAGE*)((LPBYTE)pVal+nValLen);
1802 HWND hlbox = GetDlgItem(hwnd, IDC_LIST_PROP_VERSION_TYPES);
1804 /* Read the file description for each language and code page. */
1805 for(; pTranslate<pEnd; ++pTranslate) {
1806 LPCSTR* p;
1808 for(p=InfoStrings; *p; ++p) {
1809 WCHAR subblock[200];
1810 WCHAR infoStr[100];
1811 LPCWSTR pTxt;
1812 UINT nValLen;
1814 LPCSTR pInfoString = *p;
1815 MultiByteToWideChar(CP_ACP, 0, pInfoString, -1, infoStr, 100);
1816 wsprintfW(subblock, sStringFileInfo, pTranslate->wLanguage, pTranslate->wCodePage, infoStr);
1818 /* Retrieve file description for language and code page */
1819 if (VerQueryValueW(dlg->pVersionData, subblock, (PVOID)&pTxt, &nValLen)) {
1820 int idx = SendMessageW(hlbox, LB_ADDSTRING, 0L, (LPARAM)infoStr);
1821 SendMessageW(hlbox, LB_SETITEMDATA, idx, (LPARAM)pTxt);
1826 SendMessageW(hlbox, LB_SETCURSEL, 0, 0);
1828 PropDlg_DisplayValue(hlbox, GetDlgItem(hwnd,IDC_LIST_PROP_VERSION_VALUES));
1834 static INT_PTR CALLBACK PropertiesDialogDlgProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam)
1836 static struct PropertiesDialog* dlg;
1838 switch(nmsg) {
1839 case WM_INITDIALOG: {
1840 static const WCHAR sByteFmt[] = {'%','s',' ','B','y','t','e','s','\0'};
1841 WCHAR b1[BUFFER_LEN], b2[BUFFER_LEN];
1842 LPWIN32_FIND_DATAW pWFD;
1844 dlg = (struct PropertiesDialog*) lparam;
1845 pWFD = (LPWIN32_FIND_DATAW)&dlg->entry.data;
1847 GetWindowTextW(hwnd, b1, MAX_PATH);
1848 wsprintfW(b2, b1, pWFD->cFileName);
1849 SetWindowTextW(hwnd, b2);
1851 format_date(&pWFD->ftLastWriteTime, b1, COL_DATE|COL_TIME);
1852 SetWindowTextW(GetDlgItem(hwnd, IDC_STATIC_PROP_LASTCHANGE), b1);
1854 format_longlong( b1, ((ULONGLONG)pWFD->nFileSizeHigh << 32) | pWFD->nFileSizeLow );
1855 wsprintfW(b2, sByteFmt, b1);
1856 SetWindowTextW(GetDlgItem(hwnd, IDC_STATIC_PROP_SIZE), b2);
1858 SetWindowTextW(GetDlgItem(hwnd, IDC_STATIC_PROP_FILENAME), pWFD->cFileName);
1859 SetWindowTextW(GetDlgItem(hwnd, IDC_STATIC_PROP_PATH), dlg->path);
1861 set_check(hwnd, IDC_CHECK_READONLY, pWFD->dwFileAttributes&FILE_ATTRIBUTE_READONLY);
1862 set_check(hwnd, IDC_CHECK_ARCHIVE, pWFD->dwFileAttributes&FILE_ATTRIBUTE_ARCHIVE);
1863 set_check(hwnd, IDC_CHECK_COMPRESSED, pWFD->dwFileAttributes&FILE_ATTRIBUTE_COMPRESSED);
1864 set_check(hwnd, IDC_CHECK_HIDDEN, pWFD->dwFileAttributes&FILE_ATTRIBUTE_HIDDEN);
1865 set_check(hwnd, IDC_CHECK_SYSTEM, pWFD->dwFileAttributes&FILE_ATTRIBUTE_SYSTEM);
1867 CheckForFileInfo(dlg, hwnd, dlg->path);
1868 return 1;}
1870 case WM_COMMAND: {
1871 int id = (int)wparam;
1873 switch(HIWORD(wparam)) {
1874 case LBN_SELCHANGE: {
1875 HWND hlbox = GetDlgItem(hwnd, IDC_LIST_PROP_VERSION_TYPES);
1876 PropDlg_DisplayValue(hlbox, GetDlgItem(hwnd,IDC_LIST_PROP_VERSION_VALUES));
1877 break;
1880 case BN_CLICKED:
1881 if (id==IDOK || id==IDCANCEL)
1882 EndDialog(hwnd, id);
1885 return 1;}
1887 case WM_NCDESTROY:
1888 HeapFree(GetProcessHeap(), 0, dlg->pVersionData);
1889 dlg->pVersionData = NULL;
1890 break;
1893 return 0;
1896 static void show_properties_dlg(Entry* entry, HWND hwnd)
1898 struct PropertiesDialog dlg;
1900 memset(&dlg, 0, sizeof(struct PropertiesDialog));
1901 get_path(entry, dlg.path);
1902 memcpy(&dlg.entry, entry, sizeof(Entry));
1904 DialogBoxParamW(Globals.hInstance, MAKEINTRESOURCEW(IDD_DIALOG_PROPERTIES), hwnd, PropertiesDialogDlgProc, (LPARAM)&dlg);
1907 static struct FullScreenParameters {
1908 BOOL mode;
1909 RECT orgPos;
1910 BOOL wasZoomed;
1911 } g_fullscreen = {
1912 FALSE, /* mode */
1913 {0, 0, 0, 0},
1914 FALSE
1917 static void frame_get_clientspace(HWND hwnd, PRECT prect)
1919 RECT rt;
1921 if (!IsIconic(hwnd))
1922 GetClientRect(hwnd, prect);
1923 else {
1924 WINDOWPLACEMENT wp;
1926 GetWindowPlacement(hwnd, &wp);
1928 prect->left = prect->top = 0;
1929 prect->right = wp.rcNormalPosition.right-wp.rcNormalPosition.left-
1930 2*(GetSystemMetrics(SM_CXSIZEFRAME)+GetSystemMetrics(SM_CXEDGE));
1931 prect->bottom = wp.rcNormalPosition.bottom-wp.rcNormalPosition.top-
1932 2*(GetSystemMetrics(SM_CYSIZEFRAME)+GetSystemMetrics(SM_CYEDGE))-
1933 GetSystemMetrics(SM_CYCAPTION)-GetSystemMetrics(SM_CYMENUSIZE);
1936 if (IsWindowVisible(Globals.htoolbar)) {
1937 GetClientRect(Globals.htoolbar, &rt);
1938 prect->top += rt.bottom+2;
1941 if (IsWindowVisible(Globals.hdrivebar)) {
1942 GetClientRect(Globals.hdrivebar, &rt);
1943 prect->top += rt.bottom+2;
1946 if (IsWindowVisible(Globals.hstatusbar)) {
1947 GetClientRect(Globals.hstatusbar, &rt);
1948 prect->bottom -= rt.bottom;
1952 static BOOL toggle_fullscreen(HWND hwnd)
1954 RECT rt;
1956 if ((g_fullscreen.mode=!g_fullscreen.mode)) {
1957 GetWindowRect(hwnd, &g_fullscreen.orgPos);
1958 g_fullscreen.wasZoomed = IsZoomed(hwnd);
1960 Frame_CalcFrameClient(hwnd, &rt);
1961 MapWindowPoints( hwnd, 0, (POINT *)&rt, 2 );
1963 rt.left = g_fullscreen.orgPos.left-rt.left;
1964 rt.top = g_fullscreen.orgPos.top-rt.top;
1965 rt.right = GetSystemMetrics(SM_CXSCREEN)+g_fullscreen.orgPos.right-rt.right;
1966 rt.bottom = GetSystemMetrics(SM_CYSCREEN)+g_fullscreen.orgPos.bottom-rt.bottom;
1968 MoveWindow(hwnd, rt.left, rt.top, rt.right-rt.left, rt.bottom-rt.top, TRUE);
1969 } else {
1970 MoveWindow(hwnd, g_fullscreen.orgPos.left, g_fullscreen.orgPos.top,
1971 g_fullscreen.orgPos.right-g_fullscreen.orgPos.left,
1972 g_fullscreen.orgPos.bottom-g_fullscreen.orgPos.top, TRUE);
1974 if (g_fullscreen.wasZoomed)
1975 ShowWindow(hwnd, WS_MAXIMIZE);
1978 return g_fullscreen.mode;
1981 static void fullscreen_move(HWND hwnd)
1983 RECT rt, pos;
1984 GetWindowRect(hwnd, &pos);
1986 Frame_CalcFrameClient(hwnd, &rt);
1987 MapWindowPoints( hwnd, 0, (POINT *)&rt, 2 );
1989 rt.left = pos.left-rt.left;
1990 rt.top = pos.top-rt.top;
1991 rt.right = GetSystemMetrics(SM_CXSCREEN)+pos.right-rt.right;
1992 rt.bottom = GetSystemMetrics(SM_CYSCREEN)+pos.bottom-rt.bottom;
1994 MoveWindow(hwnd, rt.left, rt.top, rt.right-rt.left, rt.bottom-rt.top, TRUE);
1997 static void toggle_child(HWND hwnd, UINT cmd, HWND hchild)
1999 BOOL vis = IsWindowVisible(hchild);
2001 CheckMenuItem(Globals.hMenuOptions, cmd, vis?MF_BYCOMMAND:MF_BYCOMMAND|MF_CHECKED);
2003 ShowWindow(hchild, vis?SW_HIDE:SW_SHOW);
2005 if (g_fullscreen.mode)
2006 fullscreen_move(hwnd);
2008 resize_frame_client(hwnd);
2011 static BOOL activate_drive_window(LPCWSTR path)
2013 WCHAR drv1[_MAX_DRIVE], drv2[_MAX_DRIVE];
2014 HWND child_wnd;
2016 _wsplitpath(path, drv1, 0, 0, 0);
2018 /* search for an already open window for the same drive */
2019 for(child_wnd=GetNextWindow(Globals.hmdiclient,GW_CHILD); child_wnd; child_wnd=GetNextWindow(child_wnd, GW_HWNDNEXT)) {
2020 ChildWnd* child = (ChildWnd*)GetWindowLongPtrW(child_wnd, GWLP_USERDATA);
2022 if (child) {
2023 _wsplitpath(child->root.path, drv2, 0, 0, 0);
2025 if (!lstrcmpiW(drv2, drv1)) {
2026 SendMessageW(Globals.hmdiclient, WM_MDIACTIVATE, (WPARAM)child_wnd, 0);
2028 if (IsIconic(child_wnd))
2029 ShowWindow(child_wnd, SW_SHOWNORMAL);
2031 return TRUE;
2036 return FALSE;
2039 static BOOL activate_fs_window(LPCWSTR filesys)
2041 HWND child_wnd;
2043 /* search for an already open window of the given file system name */
2044 for(child_wnd=GetNextWindow(Globals.hmdiclient,GW_CHILD); child_wnd; child_wnd=GetNextWindow(child_wnd, GW_HWNDNEXT)) {
2045 ChildWnd* child = (ChildWnd*) GetWindowLongPtrW(child_wnd, GWLP_USERDATA);
2047 if (child) {
2048 if (!lstrcmpiW(child->root.fs, filesys)) {
2049 SendMessageW(Globals.hmdiclient, WM_MDIACTIVATE, (WPARAM)child_wnd, 0);
2051 if (IsIconic(child_wnd))
2052 ShowWindow(child_wnd, SW_SHOWNORMAL);
2054 return TRUE;
2059 return FALSE;
2062 static LRESULT CALLBACK FrameWndProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam)
2064 WCHAR b1[BUFFER_LEN], b2[BUFFER_LEN];
2066 switch(nmsg) {
2067 case WM_CLOSE:
2068 if (Globals.saveSettings)
2069 save_registry_settings();
2071 DestroyWindow(hwnd);
2073 /* clear handle variables */
2074 Globals.hMenuFrame = 0;
2075 Globals.hMenuView = 0;
2076 Globals.hMenuOptions = 0;
2077 Globals.hMainWnd = 0;
2078 Globals.hmdiclient = 0;
2079 Globals.hdrivebar = 0;
2080 break;
2082 case WM_DESTROY:
2083 PostQuitMessage(0);
2084 break;
2086 case WM_INITMENUPOPUP: {
2087 HWND hwndClient = (HWND)SendMessageW(Globals.hmdiclient, WM_MDIGETACTIVE, 0, 0);
2089 if (!SendMessageW(hwndClient, WM_INITMENUPOPUP, wparam, lparam))
2090 return 0;
2091 break;}
2093 case WM_COMMAND: {
2094 UINT cmd = LOWORD(wparam);
2095 HWND hwndClient = (HWND)SendMessageW(Globals.hmdiclient, WM_MDIGETACTIVE, 0, 0);
2097 if (SendMessageW(hwndClient, WM_DISPATCH_COMMAND, wparam, lparam))
2098 break;
2100 if (cmd>=ID_DRIVE_FIRST && cmd<=ID_DRIVE_FIRST+0xFF) {
2101 WCHAR drv[_MAX_DRIVE], path[MAX_PATH];
2102 ChildWnd* child;
2103 LPCWSTR root = Globals.drives;
2104 int i;
2106 for(i=cmd-ID_DRIVE_FIRST; i--; root++)
2107 while(*root)
2108 root++;
2110 if (activate_drive_window(root))
2111 return 0;
2113 _wsplitpath(root, drv, 0, 0, 0);
2115 if (!SetCurrentDirectoryW(drv)) {
2116 display_error(hwnd, GetLastError());
2117 return 0;
2120 GetCurrentDirectoryW(MAX_PATH, path); /*TODO: store last directory per drive */
2121 child = alloc_child_window(path, NULL, hwnd);
2123 if (!create_child_window(child))
2124 HeapFree(GetProcessHeap(), 0, child);
2125 } else switch(cmd) {
2126 case ID_FILE_EXIT:
2127 SendMessageW(hwnd, WM_CLOSE, 0, 0);
2128 break;
2130 case ID_WINDOW_NEW: {
2131 WCHAR path[MAX_PATH];
2132 ChildWnd* child;
2134 GetCurrentDirectoryW(MAX_PATH, path);
2135 child = alloc_child_window(path, NULL, hwnd);
2137 if (!create_child_window(child))
2138 HeapFree(GetProcessHeap(), 0, child);
2139 break;}
2141 case ID_REFRESH:
2142 refresh_drives();
2143 break;
2145 case ID_WINDOW_CASCADE:
2146 SendMessageW(Globals.hmdiclient, WM_MDICASCADE, 0, 0);
2147 break;
2149 case ID_WINDOW_TILE_HORZ:
2150 SendMessageW(Globals.hmdiclient, WM_MDITILE, MDITILE_HORIZONTAL, 0);
2151 break;
2153 case ID_WINDOW_TILE_VERT:
2154 SendMessageW(Globals.hmdiclient, WM_MDITILE, MDITILE_VERTICAL, 0);
2155 break;
2157 case ID_WINDOW_ARRANGE:
2158 SendMessageW(Globals.hmdiclient, WM_MDIICONARRANGE, 0, 0);
2159 break;
2161 case ID_SELECT_FONT:
2162 choose_font(hwnd);
2163 break;
2165 case ID_VIEW_TOOL_BAR:
2166 toggle_child(hwnd, cmd, Globals.htoolbar);
2167 break;
2169 case ID_VIEW_DRIVE_BAR:
2170 toggle_child(hwnd, cmd, Globals.hdrivebar);
2171 break;
2173 case ID_VIEW_STATUSBAR:
2174 toggle_child(hwnd, cmd, Globals.hstatusbar);
2175 break;
2177 case ID_VIEW_SAVESETTINGS:
2178 Globals.saveSettings = !Globals.saveSettings;
2179 CheckMenuItem(Globals.hMenuOptions, ID_VIEW_SAVESETTINGS,
2180 Globals.saveSettings ? MF_CHECKED : MF_UNCHECKED );
2181 break;
2183 case ID_RUN:
2184 WineFile_OnRun( hwnd );
2185 break;
2187 case ID_CONNECT_NETWORK_DRIVE: {
2188 DWORD ret = WNetConnectionDialog(hwnd, RESOURCETYPE_DISK);
2189 if (ret == NO_ERROR)
2190 refresh_drives();
2191 else if (ret != (DWORD)-1) {
2192 if (ret == ERROR_EXTENDED_ERROR)
2193 display_network_error(hwnd);
2194 else
2195 display_error(hwnd, ret);
2197 break;}
2199 case ID_DISCONNECT_NETWORK_DRIVE: {
2200 DWORD ret = WNetDisconnectDialog(hwnd, RESOURCETYPE_DISK);
2201 if (ret == NO_ERROR)
2202 refresh_drives();
2203 else if (ret != (DWORD)-1) {
2204 if (ret == ERROR_EXTENDED_ERROR)
2205 display_network_error(hwnd);
2206 else
2207 display_error(hwnd, ret);
2209 break;}
2211 case ID_HELP:
2212 WinHelpW(hwnd, RS(b1,IDS_WINEFILE), HELP_INDEX, 0);
2213 break;
2215 case ID_VIEW_FULLSCREEN:
2216 CheckMenuItem(Globals.hMenuOptions, cmd, toggle_fullscreen(hwnd)?MF_CHECKED:0);
2217 break;
2219 #ifdef __WINE__
2220 case ID_DRIVE_UNIX_FS: {
2221 WCHAR path[MAX_PATH];
2222 char cpath[MAX_PATH];
2223 ChildWnd* child;
2225 if (activate_fs_window(RS(b1,IDS_UNIXFS)))
2226 break;
2228 getcwd(cpath, MAX_PATH);
2229 MultiByteToWideChar(CP_UNIXCP, 0, cpath, -1, path, MAX_PATH);
2230 child = alloc_child_window(path, NULL, hwnd);
2232 if (!create_child_window(child))
2233 HeapFree(GetProcessHeap(), 0, child);
2234 break;}
2235 #endif
2236 case ID_DRIVE_SHELL_NS: {
2237 WCHAR path[MAX_PATH];
2238 ChildWnd* child;
2240 if (activate_fs_window(RS(b1,IDS_SHELL)))
2241 break;
2243 GetCurrentDirectoryW(MAX_PATH, path);
2244 child = alloc_child_window(path, get_path_pidl(path,hwnd), hwnd);
2246 if (!create_child_window(child))
2247 HeapFree(GetProcessHeap(), 0, child);
2248 break;}
2250 /*TODO: There are even more menu items! */
2252 case ID_ABOUT:
2253 ShellAboutW(hwnd, RS(b1,IDS_WINEFILE), NULL,
2254 LoadImageW( Globals.hInstance, MAKEINTRESOURCEW(IDI_WINEFILE),
2255 IMAGE_ICON, 48, 48, LR_SHARED ));
2256 break;
2258 default:
2259 /*TODO: if (wParam >= PM_FIRST_LANGUAGE && wParam <= PM_LAST_LANGUAGE)
2260 STRING_SelectLanguageByNumber(wParam - PM_FIRST_LANGUAGE);
2261 else */if ((cmd<IDW_FIRST_CHILD || cmd>=IDW_FIRST_CHILD+0x100) &&
2262 (cmd<SC_SIZE || cmd>SC_RESTORE))
2263 MessageBoxW(hwnd, RS(b2,IDS_NO_IMPL), RS(b1,IDS_WINEFILE), MB_OK);
2265 return DefFrameProcW(hwnd, Globals.hmdiclient, nmsg, wparam, lparam);
2267 break;}
2269 case WM_SIZE:
2270 resize_frame(hwnd, LOWORD(lparam), HIWORD(lparam));
2271 break; /* do not pass message to DefFrameProcW */
2273 case WM_DEVICECHANGE:
2274 SendMessageW(hwnd, WM_COMMAND, MAKELONG(ID_REFRESH,0), 0);
2275 break;
2277 case WM_GETMINMAXINFO: {
2278 LPMINMAXINFO lpmmi = (LPMINMAXINFO)lparam;
2280 lpmmi->ptMaxTrackSize.x <<= 1;/*2*GetSystemMetrics(SM_CXSCREEN) / SM_CXVIRTUALSCREEN */
2281 lpmmi->ptMaxTrackSize.y <<= 1;/*2*GetSystemMetrics(SM_CYSCREEN) / SM_CYVIRTUALSCREEN */
2282 break;}
2284 case FRM_CALC_CLIENT:
2285 frame_get_clientspace(hwnd, (PRECT)lparam);
2286 return TRUE;
2288 default:
2289 return DefFrameProcW(hwnd, Globals.hmdiclient, nmsg, wparam, lparam);
2292 return 0;
2296 static WCHAR g_pos_names[COLUMNS][40] = {
2297 {'\0'} /* symbol */
2300 static const int g_pos_align[] = {
2302 HDF_LEFT, /* Name */
2303 HDF_RIGHT, /* Size */
2304 HDF_LEFT, /* CDate */
2305 HDF_LEFT, /* ADate */
2306 HDF_LEFT, /* MDate */
2307 HDF_LEFT, /* Index */
2308 HDF_CENTER, /* Links */
2309 HDF_CENTER, /* Attributes */
2310 HDF_LEFT /* Security */
2313 static void resize_tree(ChildWnd* child, int cx, int cy)
2315 HDWP hdwp = BeginDeferWindowPos(4);
2316 RECT rt;
2317 WINDOWPOS wp;
2318 HD_LAYOUT hdl;
2320 rt.left = 0;
2321 rt.top = 0;
2322 rt.right = cx;
2323 rt.bottom = cy;
2325 cx = child->split_pos + SPLIT_WIDTH/2;
2326 hdl.prc = &rt;
2327 hdl.pwpos = &wp;
2329 SendMessageW(child->left.hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hdl);
2331 DeferWindowPos(hdwp, child->left.hwndHeader, wp.hwndInsertAfter,
2332 wp.x-1, wp.y, child->split_pos-SPLIT_WIDTH/2+1, wp.cy, wp.flags);
2333 DeferWindowPos(hdwp, child->right.hwndHeader, wp.hwndInsertAfter,
2334 rt.left+cx+1, wp.y, wp.cx-cx+2, wp.cy, wp.flags);
2335 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);
2336 DeferWindowPos(hdwp, child->right.hwnd, 0, rt.left+cx+1, rt.top, rt.right-cx, rt.bottom-rt.top, SWP_NOZORDER|SWP_NOACTIVATE);
2338 EndDeferWindowPos(hdwp);
2341 static HWND create_header(HWND parent, Pane* pane, UINT id)
2343 HDITEMW hdi;
2344 int idx;
2346 HWND hwnd = CreateWindowW(WC_HEADERW, 0, WS_CHILD|WS_VISIBLE|HDS_HORZ|HDS_FULLDRAG/*TODO: |HDS_BUTTONS + sort orders*/,
2347 0, 0, 0, 0, parent, (HMENU)ULongToHandle(id), Globals.hInstance, 0);
2348 if (!hwnd)
2349 return 0;
2351 SendMessageW(hwnd, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), FALSE);
2353 hdi.mask = HDI_TEXT|HDI_WIDTH|HDI_FORMAT;
2355 for(idx=0; idx<COLUMNS; idx++) {
2356 hdi.pszText = g_pos_names[idx];
2357 hdi.fmt = HDF_STRING | g_pos_align[idx];
2358 hdi.cxy = pane->widths[idx];
2359 pane->widths_shown[idx] = hdi.cxy;
2360 SendMessageW(hwnd, HDM_INSERTITEMW, idx, (LPARAM)&hdi);
2363 return hwnd;
2366 static void init_output(HWND hwnd)
2368 static const WCHAR s1000[] = {'1','0','0','0','\0'};
2369 WCHAR b[16];
2370 HFONT old_font;
2371 HDC hdc = GetDC(hwnd);
2373 if (GetNumberFormatW(LOCALE_USER_DEFAULT, 0, s1000, 0, b, 16) > 4)
2374 Globals.num_sep = b[1];
2375 else
2376 Globals.num_sep = '.';
2378 old_font = SelectObject(hdc, Globals.hfont);
2379 GetTextExtentPoint32W(hdc, sSpace, 1, &Globals.spaceSize);
2380 SelectObject(hdc, old_font);
2381 ReleaseDC(hwnd, hdc);
2384 static void draw_item(Pane* pane, LPDRAWITEMSTRUCT dis, Entry* entry, int calcWidthCol);
2387 /* calculate preferred width for all visible columns */
2389 static BOOL calc_widths(Pane* pane, BOOL anyway)
2391 int col, x, cx, spc=3*Globals.spaceSize.cx;
2392 int entries = SendMessageW(pane->hwnd, LB_GETCOUNT, 0, 0);
2393 int orgWidths[COLUMNS];
2394 int orgPositions[COLUMNS+1];
2395 HFONT hfontOld;
2396 HDC hdc;
2397 int cnt;
2399 if (!anyway) {
2400 memcpy(orgWidths, pane->widths, sizeof(orgWidths));
2401 memcpy(orgPositions, pane->positions, sizeof(orgPositions));
2404 for(col=0; col<COLUMNS; col++)
2405 pane->widths[col] = 0;
2407 hdc = GetDC(pane->hwnd);
2408 hfontOld = SelectObject(hdc, Globals.hfont);
2410 for(cnt=0; cnt<entries; cnt++) {
2411 Entry* entry = (Entry*)SendMessageW(pane->hwnd, LB_GETITEMDATA, cnt, 0);
2413 DRAWITEMSTRUCT dis;
2415 dis.CtlType = 0;
2416 dis.CtlID = 0;
2417 dis.itemID = 0;
2418 dis.itemAction = 0;
2419 dis.itemState = 0;
2420 dis.hwndItem = pane->hwnd;
2421 dis.hDC = hdc;
2422 SetRectEmpty(&dis.rcItem);
2423 /*dis.itemData = 0; */
2425 draw_item(pane, &dis, entry, COLUMNS);
2428 SelectObject(hdc, hfontOld);
2429 ReleaseDC(pane->hwnd, hdc);
2431 x = 0;
2432 for(col=0; col<COLUMNS; col++) {
2433 pane->positions[col] = x;
2434 cx = pane->widths[col];
2436 if (cx) {
2437 cx += spc;
2439 if (cx < IMAGE_WIDTH)
2440 cx = IMAGE_WIDTH;
2442 pane->widths[col] = cx;
2445 x += cx;
2448 pane->positions[COLUMNS] = x;
2450 SendMessageW(pane->hwnd, LB_SETHORIZONTALEXTENT, x, 0);
2452 /* no change? */
2453 if (!anyway && !memcmp(orgWidths, pane->widths, sizeof(orgWidths)))
2454 return FALSE;
2456 /* don't move, if only collapsing an entry */
2457 if (!anyway && pane->widths[0]<orgWidths[0] &&
2458 !memcmp(orgWidths+1, pane->widths+1, sizeof(orgWidths)-sizeof(int))) {
2459 pane->widths[0] = orgWidths[0];
2460 memcpy(pane->positions, orgPositions, sizeof(orgPositions));
2462 return FALSE;
2465 InvalidateRect(pane->hwnd, 0, TRUE);
2467 return TRUE;
2470 /* calculate one preferred column width */
2471 static void calc_single_width(Pane* pane, int col)
2473 HFONT hfontOld;
2474 int x, cx;
2475 int entries = SendMessageW(pane->hwnd, LB_GETCOUNT, 0, 0);
2476 int cnt;
2477 HDC hdc;
2479 pane->widths[col] = 0;
2481 hdc = GetDC(pane->hwnd);
2482 hfontOld = SelectObject(hdc, Globals.hfont);
2484 for(cnt=0; cnt<entries; cnt++) {
2485 Entry* entry = (Entry*)SendMessageW(pane->hwnd, LB_GETITEMDATA, cnt, 0);
2486 DRAWITEMSTRUCT dis;
2488 dis.CtlType = 0;
2489 dis.CtlID = 0;
2490 dis.itemID = 0;
2491 dis.itemAction = 0;
2492 dis.itemState = 0;
2493 dis.hwndItem = pane->hwnd;
2494 dis.hDC = hdc;
2495 SetRectEmpty(&dis.rcItem);
2496 /*dis.itemData = 0; */
2498 draw_item(pane, &dis, entry, col);
2501 SelectObject(hdc, hfontOld);
2502 ReleaseDC(pane->hwnd, hdc);
2504 cx = pane->widths[col];
2506 if (cx) {
2507 cx += 3*Globals.spaceSize.cx;
2509 if (cx < IMAGE_WIDTH)
2510 cx = IMAGE_WIDTH;
2513 pane->widths[col] = cx;
2515 x = pane->positions[col] + cx;
2517 for(; col<COLUMNS-1; ) {
2518 pane->positions[++col] = x;
2519 x += pane->widths[col];
2522 SendMessageW(pane->hwnd, LB_SETHORIZONTALEXTENT, x, 0);
2525 static BOOL pattern_match(LPCWSTR str, LPCWSTR pattern)
2527 for( ; *str&&*pattern; str++,pattern++) {
2528 if (*pattern == '*') {
2529 do pattern++;
2530 while(*pattern == '*');
2532 if (!*pattern)
2533 return TRUE;
2535 for(; *str; str++)
2536 if (*str==*pattern && pattern_match(str, pattern))
2537 return TRUE;
2539 return FALSE;
2541 else if (*str!=*pattern && *pattern!='?')
2542 return FALSE;
2545 if (*str || *pattern)
2546 if (*pattern!='*' || pattern[1]!='\0')
2547 return FALSE;
2549 return TRUE;
2552 static BOOL pattern_imatch(LPCWSTR str, LPCWSTR pattern)
2554 WCHAR b1[BUFFER_LEN], b2[BUFFER_LEN];
2556 lstrcpyW(b1, str);
2557 lstrcpyW(b2, pattern);
2558 CharUpperW(b1);
2559 CharUpperW(b2);
2561 return pattern_match(b1, b2);
2565 enum FILE_TYPE {
2566 FT_OTHER = 0,
2567 FT_EXECUTABLE = 1,
2568 FT_DOCUMENT = 2
2571 static enum FILE_TYPE get_file_type(LPCWSTR filename);
2574 /* insert listbox entries after index idx */
2576 static int insert_entries(Pane* pane, Entry* dir, LPCWSTR pattern, int filter_flags, int idx)
2578 Entry* entry = dir;
2580 if (!entry)
2581 return idx;
2583 ShowWindow(pane->hwnd, SW_HIDE);
2585 for(; entry; entry=entry->next) {
2586 if (pane->treePane && !(entry->data.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY))
2587 continue;
2589 if (entry->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
2590 /* don't display entries "." and ".." in the left pane */
2591 if (pane->treePane && entry->data.cFileName[0] == '.')
2592 if (entry->data.cFileName[1] == '\0' ||
2593 (entry->data.cFileName[1] == '.' &&
2594 entry->data.cFileName[2] == '\0'))
2595 continue;
2597 /* filter directories in right pane */
2598 if (!pane->treePane && !(filter_flags&TF_DIRECTORIES))
2599 continue;
2602 /* filter using the file name pattern */
2603 if (pattern)
2604 if (!pattern_imatch(entry->data.cFileName, pattern))
2605 continue;
2607 /* filter system and hidden files */
2608 if (!(filter_flags&TF_HIDDEN) && (entry->data.dwFileAttributes&(FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM)))
2609 continue;
2611 /* filter looking at the file type */
2612 if ((filter_flags&(TF_PROGRAMS|TF_DOCUMENTS|TF_OTHERS)) != (TF_PROGRAMS|TF_DOCUMENTS|TF_OTHERS))
2613 switch(get_file_type(entry->data.cFileName)) {
2614 case FT_EXECUTABLE:
2615 if (!(filter_flags & TF_PROGRAMS))
2616 continue;
2617 break;
2619 case FT_DOCUMENT:
2620 if (!(filter_flags & TF_DOCUMENTS))
2621 continue;
2622 break;
2624 default: /* TF_OTHERS */
2625 if (!(filter_flags & TF_OTHERS))
2626 continue;
2629 if (idx != -1)
2630 idx++;
2632 SendMessageW(pane->hwnd, LB_INSERTSTRING, idx, (LPARAM)entry);
2634 if (pane->treePane && entry->expanded)
2635 idx = insert_entries(pane, entry->down, pattern, filter_flags, idx);
2638 ShowWindow(pane->hwnd, SW_SHOW);
2640 return idx;
2643 static void set_space_status(void)
2645 ULARGE_INTEGER ulFreeBytesToCaller, ulTotalBytes, ulFreeBytes;
2646 WCHAR fmt[64], b1[64], b2[64], buffer[BUFFER_LEN];
2648 if (GetDiskFreeSpaceExW(NULL, &ulFreeBytesToCaller, &ulTotalBytes, &ulFreeBytes)) {
2649 DWORD_PTR args[2];
2651 args[0] = (DWORD_PTR)StrFormatByteSizeW(ulFreeBytesToCaller.QuadPart, b1, sizeof(b1)/sizeof(*b1));
2652 args[1] = (DWORD_PTR)StrFormatByteSizeW(ulTotalBytes.QuadPart, b2, sizeof(b2)/sizeof(*b2));
2654 FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
2655 RS(fmt,IDS_FREE_SPACE_FMT), 0, 0, buffer,
2656 sizeof(buffer)/sizeof(*buffer), (__ms_va_list*)args);
2657 } else
2658 lstrcpyW(buffer, sQMarks);
2660 SendMessageW(Globals.hstatusbar, SB_SETTEXTW, 0, (LPARAM)buffer);
2664 static WNDPROC g_orgTreeWndProc;
2666 static void create_tree_window(HWND parent, Pane* pane, UINT id, UINT id_header, LPCWSTR pattern, int filter_flags)
2668 static const WCHAR sListBox[] = {'L','i','s','t','B','o','x','\0'};
2670 static BOOL s_init = FALSE;
2671 Entry* entry = pane->root;
2673 pane->hwnd = CreateWindowW(sListBox, sEmpty, WS_CHILD|WS_VISIBLE|WS_HSCROLL|WS_VSCROLL|
2674 LBS_DISABLENOSCROLL|LBS_NOINTEGRALHEIGHT|LBS_OWNERDRAWFIXED|LBS_NOTIFY,
2675 0, 0, 0, 0, parent, (HMENU)ULongToHandle(id), Globals.hInstance, 0);
2677 SetWindowLongPtrW(pane->hwnd, GWLP_USERDATA, (LPARAM)pane);
2678 g_orgTreeWndProc = (WNDPROC)SetWindowLongPtrW(pane->hwnd, GWLP_WNDPROC, (LPARAM)TreeWndProc);
2680 SendMessageW(pane->hwnd, WM_SETFONT, (WPARAM)Globals.hfont, FALSE);
2682 /* insert entries into listbox */
2683 if (entry)
2684 insert_entries(pane, entry, pattern, filter_flags, -1);
2686 /* calculate column widths */
2687 if (!s_init) {
2688 s_init = TRUE;
2689 init_output(pane->hwnd);
2692 calc_widths(pane, TRUE);
2694 pane->hwndHeader = create_header(parent, pane, id_header);
2698 static void InitChildWindow(ChildWnd* child)
2700 create_tree_window(child->hwnd, &child->left, IDW_TREE_LEFT, IDW_HEADER_LEFT, NULL, TF_ALL);
2701 create_tree_window(child->hwnd, &child->right, IDW_TREE_RIGHT, IDW_HEADER_RIGHT, child->filter_pattern, child->filter_flags);
2705 static void format_date(const FILETIME* ft, WCHAR* buffer, int visible_cols)
2707 SYSTEMTIME systime;
2708 FILETIME lft;
2709 int len = 0;
2711 *buffer = '\0';
2713 if (!ft->dwLowDateTime && !ft->dwHighDateTime)
2714 return;
2716 if (!FileTimeToLocalFileTime(ft, &lft))
2717 {err: lstrcpyW(buffer,sQMarks); return;}
2719 if (!FileTimeToSystemTime(&lft, &systime))
2720 goto err;
2722 if (visible_cols & COL_DATE) {
2723 len = GetDateFormatW(LOCALE_USER_DEFAULT, 0, &systime, 0, buffer, BUFFER_LEN);
2724 if (!len)
2725 goto err;
2728 if (visible_cols & COL_TIME) {
2729 if (len)
2730 buffer[len-1] = ' ';
2732 buffer[len++] = ' ';
2734 if (!GetTimeFormatW(LOCALE_USER_DEFAULT, 0, &systime, 0, buffer+len, BUFFER_LEN-len))
2735 buffer[len] = '\0';
2740 static void calc_width(Pane* pane, LPDRAWITEMSTRUCT dis, int col, LPCWSTR str)
2742 RECT rt = {0, 0, 0, 0};
2744 DrawTextW(dis->hDC, str, -1, &rt, DT_CALCRECT|DT_SINGLELINE|DT_NOPREFIX);
2746 if (rt.right > pane->widths[col])
2747 pane->widths[col] = rt.right;
2750 static void calc_tabbed_width(Pane* pane, LPDRAWITEMSTRUCT dis, int col, LPCWSTR str)
2752 RECT rt = {0, 0, 0, 0};
2754 DrawTextW(dis->hDC, str, -1, &rt, DT_CALCRECT|DT_SINGLELINE|DT_EXPANDTABS|DT_TABSTOP|(2<<8));
2755 /*FIXME rt (0,0) ??? */
2757 if (rt.right > pane->widths[col])
2758 pane->widths[col] = rt.right;
2762 static void output_text(Pane* pane, LPDRAWITEMSTRUCT dis, int col, LPCWSTR str, DWORD flags)
2764 int x = dis->rcItem.left;
2765 RECT rt;
2767 rt.left = x+pane->positions[col]+Globals.spaceSize.cx;
2768 rt.top = dis->rcItem.top;
2769 rt.right = x+pane->positions[col+1]-Globals.spaceSize.cx;
2770 rt.bottom = dis->rcItem.bottom;
2772 DrawTextW(dis->hDC, str, -1, &rt, DT_SINGLELINE|DT_NOPREFIX|flags);
2775 static void output_tabbed_text(Pane* pane, LPDRAWITEMSTRUCT dis, int col, LPCWSTR str)
2777 int x = dis->rcItem.left;
2778 RECT rt;
2780 rt.left = x+pane->positions[col]+Globals.spaceSize.cx;
2781 rt.top = dis->rcItem.top;
2782 rt.right = x+pane->positions[col+1]-Globals.spaceSize.cx;
2783 rt.bottom = dis->rcItem.bottom;
2785 DrawTextW(dis->hDC, str, -1, &rt, DT_SINGLELINE|DT_EXPANDTABS|DT_TABSTOP|(2<<8));
2788 static void output_number(Pane* pane, LPDRAWITEMSTRUCT dis, int col, LPCWSTR str)
2790 int x = dis->rcItem.left;
2791 RECT rt;
2792 LPCWSTR s = str;
2793 WCHAR b[128];
2794 LPWSTR d = b;
2795 int pos;
2797 rt.left = x+pane->positions[col]+Globals.spaceSize.cx;
2798 rt.top = dis->rcItem.top;
2799 rt.right = x+pane->positions[col+1]-Globals.spaceSize.cx;
2800 rt.bottom = dis->rcItem.bottom;
2802 if (*s)
2803 *d++ = *s++;
2805 /* insert number separator characters */
2806 pos = lstrlenW(s) % 3;
2808 while(*s)
2809 if (pos--)
2810 *d++ = *s++;
2811 else {
2812 *d++ = Globals.num_sep;
2813 pos = 3;
2816 DrawTextW(dis->hDC, b, d-b, &rt, DT_RIGHT|DT_SINGLELINE|DT_NOPREFIX|DT_END_ELLIPSIS);
2820 static BOOL is_exe_file(LPCWSTR ext)
2822 static const WCHAR executable_extensions[][4] = {
2823 {'C','O','M','\0'},
2824 {'E','X','E','\0'},
2825 {'B','A','T','\0'},
2826 {'C','M','D','\0'},
2827 {'C','M','M','\0'},
2828 {'B','T','M','\0'},
2829 {'A','W','K','\0'},
2830 {'\0'}
2833 WCHAR ext_buffer[_MAX_EXT];
2834 const WCHAR (*p)[4];
2835 LPCWSTR s;
2836 LPWSTR d;
2838 for(s=ext+1,d=ext_buffer; (*d=tolower(*s)); s++)
2839 d++;
2841 for(p=executable_extensions; (*p)[0]; p++)
2842 if (!lstrcmpiW(ext_buffer, *p))
2843 return TRUE;
2845 return FALSE;
2848 static BOOL is_registered_type(LPCWSTR ext)
2850 /* check if there exists a classname for this file extension in the registry */
2851 if (!RegQueryValueW(HKEY_CLASSES_ROOT, ext, NULL, NULL))
2852 return TRUE;
2854 return FALSE;
2857 static enum FILE_TYPE get_file_type(LPCWSTR filename)
2859 LPCWSTR ext = strrchrW(filename, '.');
2860 if (!ext)
2861 ext = sEmpty;
2863 if (is_exe_file(ext))
2864 return FT_EXECUTABLE;
2865 else if (is_registered_type(ext))
2866 return FT_DOCUMENT;
2867 else
2868 return FT_OTHER;
2872 static void draw_item(Pane* pane, LPDRAWITEMSTRUCT dis, Entry* entry, int calcWidthCol)
2874 WCHAR buffer[BUFFER_LEN];
2875 DWORD attrs;
2876 int visible_cols = pane->visible_cols;
2877 COLORREF bkcolor, textcolor;
2878 RECT focusRect = dis->rcItem;
2879 HBRUSH hbrush;
2880 enum IMAGE img;
2881 int img_pos, cx;
2882 int col = 0;
2884 if (entry) {
2885 attrs = entry->data.dwFileAttributes;
2887 if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
2888 if (entry->data.cFileName[0] == '.' && entry->data.cFileName[1] == '.'
2889 && entry->data.cFileName[2] == '\0')
2890 img = IMG_FOLDER_UP;
2891 else if (entry->data.cFileName[0] == '.' && entry->data.cFileName[1] == '\0')
2892 img = IMG_FOLDER_CUR;
2893 else if (pane->treePane && (dis->itemState&ODS_FOCUS))
2894 img = IMG_OPEN_FOLDER;
2895 else
2896 img = IMG_FOLDER;
2897 } else {
2898 switch(get_file_type(entry->data.cFileName)) {
2899 case FT_EXECUTABLE: img = IMG_EXECUTABLE; break;
2900 case FT_DOCUMENT: img = IMG_DOCUMENT; break;
2901 default: img = IMG_FILE;
2904 } else {
2905 attrs = 0;
2906 img = IMG_NONE;
2909 if (pane->treePane) {
2910 if (entry) {
2911 img_pos = dis->rcItem.left + entry->level*(IMAGE_WIDTH+TREE_LINE_DX);
2913 if (calcWidthCol == -1) {
2914 int x;
2915 int y = dis->rcItem.top + IMAGE_HEIGHT/2;
2916 Entry* up;
2917 RECT rt_clip;
2918 HRGN hrgn_org = CreateRectRgn(0, 0, 0, 0);
2919 HRGN hrgn;
2921 rt_clip.left = dis->rcItem.left;
2922 rt_clip.top = dis->rcItem.top;
2923 rt_clip.right = dis->rcItem.left+pane->widths[col];
2924 rt_clip.bottom = dis->rcItem.bottom;
2926 hrgn = CreateRectRgnIndirect(&rt_clip);
2928 if (!GetClipRgn(dis->hDC, hrgn_org)) {
2929 DeleteObject(hrgn_org);
2930 hrgn_org = 0;
2933 ExtSelectClipRgn(dis->hDC, hrgn, RGN_AND);
2934 DeleteObject(hrgn);
2936 if ((up=entry->up) != NULL) {
2937 MoveToEx(dis->hDC, img_pos-IMAGE_WIDTH/2, y, 0);
2938 LineTo(dis->hDC, img_pos-2, y);
2940 x = img_pos - IMAGE_WIDTH/2;
2942 do {
2943 x -= IMAGE_WIDTH+TREE_LINE_DX;
2945 if (up->next
2946 && (up->next->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
2948 MoveToEx(dis->hDC, x, dis->rcItem.top, 0);
2949 LineTo(dis->hDC, x, dis->rcItem.bottom);
2951 } while((up=up->up) != NULL);
2954 x = img_pos - IMAGE_WIDTH/2;
2956 MoveToEx(dis->hDC, x, dis->rcItem.top, 0);
2957 LineTo(dis->hDC, x, y);
2959 if (entry->next
2960 && (entry->next->data.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY))
2961 LineTo(dis->hDC, x, dis->rcItem.bottom);
2963 SelectClipRgn(dis->hDC, hrgn_org);
2964 if (hrgn_org) DeleteObject(hrgn_org);
2965 } else if (calcWidthCol==col || calcWidthCol==COLUMNS) {
2966 int right = img_pos + IMAGE_WIDTH - TREE_LINE_DX;
2968 if (right > pane->widths[col])
2969 pane->widths[col] = right;
2971 } else {
2972 img_pos = dis->rcItem.left;
2974 } else {
2975 img_pos = dis->rcItem.left;
2977 if (calcWidthCol==col || calcWidthCol==COLUMNS)
2978 pane->widths[col] = IMAGE_WIDTH;
2981 if (calcWidthCol == -1) {
2982 focusRect.left = img_pos -2;
2984 if (attrs & FILE_ATTRIBUTE_COMPRESSED)
2985 textcolor = COLOR_COMPRESSED;
2986 else
2987 textcolor = RGB(0,0,0);
2989 if (dis->itemState & ODS_FOCUS) {
2990 textcolor = RGB(255,255,255);
2991 bkcolor = COLOR_SELECTION;
2992 } else {
2993 bkcolor = RGB(255,255,255);
2996 hbrush = CreateSolidBrush(bkcolor);
2997 FillRect(dis->hDC, &focusRect, hbrush);
2998 DeleteObject(hbrush);
3000 SetBkMode(dis->hDC, TRANSPARENT);
3001 SetTextColor(dis->hDC, textcolor);
3003 cx = pane->widths[col];
3005 if (cx && img!=IMG_NONE) {
3006 if (cx > IMAGE_WIDTH)
3007 cx = IMAGE_WIDTH;
3009 if (entry->hicon && entry->hicon!=(HICON)-1)
3010 DrawIconEx(dis->hDC, img_pos, dis->rcItem.top, entry->hicon, cx, GetSystemMetrics(SM_CYSMICON), 0, 0, DI_NORMAL);
3011 else
3012 ImageList_DrawEx(Globals.himl, img, dis->hDC,
3013 img_pos, dis->rcItem.top, cx,
3014 IMAGE_HEIGHT, bkcolor, CLR_DEFAULT, ILD_NORMAL);
3018 if (!entry)
3019 return;
3021 col++;
3023 /* output file name */
3024 if (calcWidthCol == -1)
3025 output_text(pane, dis, col, entry->data.cFileName, 0);
3026 else if (calcWidthCol==col || calcWidthCol==COLUMNS)
3027 calc_width(pane, dis, col, entry->data.cFileName);
3029 col++;
3031 /* display file size */
3032 if (visible_cols & COL_SIZE) {
3033 format_longlong( buffer, ((ULONGLONG)entry->data.nFileSizeHigh << 32) | entry->data.nFileSizeLow );
3035 if (calcWidthCol == -1)
3036 output_number(pane, dis, col, buffer);
3037 else if (calcWidthCol==col || calcWidthCol==COLUMNS)
3038 calc_width(pane, dis, col, buffer);/*TODO: not ever time enough */
3040 col++;
3043 /* display file date */
3044 if (visible_cols & (COL_DATE|COL_TIME)) {
3045 format_date(&entry->data.ftCreationTime, buffer, visible_cols);
3046 if (calcWidthCol == -1)
3047 output_text(pane, dis, col, buffer, 0);
3048 else if (calcWidthCol==col || calcWidthCol==COLUMNS)
3049 calc_width(pane, dis, col, buffer);
3050 col++;
3052 format_date(&entry->data.ftLastAccessTime, buffer, visible_cols);
3053 if (calcWidthCol == -1)
3054 output_text(pane, dis, col, buffer, 0);
3055 else if (calcWidthCol==col || calcWidthCol==COLUMNS)
3056 calc_width(pane, dis, col, buffer);
3057 col++;
3059 format_date(&entry->data.ftLastWriteTime, buffer, visible_cols);
3060 if (calcWidthCol == -1)
3061 output_text(pane, dis, col, buffer, 0);
3062 else if (calcWidthCol==col || calcWidthCol==COLUMNS)
3063 calc_width(pane, dis, col, buffer);
3064 col++;
3067 if (entry->bhfi_valid) {
3068 if (visible_cols & COL_INDEX) {
3069 static const WCHAR fmtlow[] = {'%','X',0};
3070 static const WCHAR fmthigh[] = {'%','X','%','0','8','X',0};
3072 if (entry->bhfi.nFileIndexHigh)
3073 wsprintfW(buffer, fmthigh,
3074 entry->bhfi.nFileIndexHigh, entry->bhfi.nFileIndexLow );
3075 else
3076 wsprintfW(buffer, fmtlow, entry->bhfi.nFileIndexLow );
3078 if (calcWidthCol == -1)
3079 output_text(pane, dis, col, buffer, DT_RIGHT);
3080 else if (calcWidthCol==col || calcWidthCol==COLUMNS)
3081 calc_width(pane, dis, col, buffer);
3083 col++;
3086 if (visible_cols & COL_LINKS) {
3087 wsprintfW(buffer, sNumFmt, entry->bhfi.nNumberOfLinks);
3089 if (calcWidthCol == -1)
3090 output_text(pane, dis, col, buffer, DT_CENTER);
3091 else if (calcWidthCol==col || calcWidthCol==COLUMNS)
3092 calc_width(pane, dis, col, buffer);
3094 col++;
3096 } else
3097 col += 2;
3099 /* show file attributes */
3100 if (visible_cols & COL_ATTRIBUTES) {
3101 static const WCHAR s11Tabs[] = {' ','\t',' ','\t',' ','\t',' ','\t',' ','\t',' ','\t',' ','\t',' ','\t',' ','\t',' ','\t',' ','\t',' ','\0'};
3102 lstrcpyW(buffer, s11Tabs);
3104 if (attrs & FILE_ATTRIBUTE_NORMAL) buffer[ 0] = 'N';
3105 else {
3106 if (attrs & FILE_ATTRIBUTE_READONLY) buffer[ 2] = 'R';
3107 if (attrs & FILE_ATTRIBUTE_HIDDEN) buffer[ 4] = 'H';
3108 if (attrs & FILE_ATTRIBUTE_SYSTEM) buffer[ 6] = 'S';
3109 if (attrs & FILE_ATTRIBUTE_ARCHIVE) buffer[ 8] = 'A';
3110 if (attrs & FILE_ATTRIBUTE_COMPRESSED) buffer[10] = 'C';
3111 if (attrs & FILE_ATTRIBUTE_DIRECTORY) buffer[12] = 'D';
3112 if (attrs & FILE_ATTRIBUTE_ENCRYPTED) buffer[14] = 'E';
3113 if (attrs & FILE_ATTRIBUTE_TEMPORARY) buffer[16] = 'T';
3114 if (attrs & FILE_ATTRIBUTE_SPARSE_FILE) buffer[18] = 'P';
3115 if (attrs & FILE_ATTRIBUTE_REPARSE_POINT) buffer[20] = 'Q';
3116 if (attrs & FILE_ATTRIBUTE_OFFLINE) buffer[22] = 'O';
3117 if (attrs & FILE_ATTRIBUTE_NOT_CONTENT_INDEXED) buffer[24] = 'X';
3120 if (calcWidthCol == -1)
3121 output_tabbed_text(pane, dis, col, buffer);
3122 else if (calcWidthCol==col || calcWidthCol==COLUMNS)
3123 calc_tabbed_width(pane, dis, col, buffer);
3125 col++;
3129 static void set_header(Pane* pane)
3131 HDITEMW item;
3132 int scroll_pos = GetScrollPos(pane->hwnd, SB_HORZ);
3133 int i;
3135 item.mask = HDI_WIDTH;
3137 for (i = 0; i < COLUMNS; ++i) {
3138 if (pane->positions[i] >= scroll_pos) {
3139 item.cxy = pane->widths[i];
3140 } else if (pane->positions[i+1] <= scroll_pos) {
3141 item.cxy = 0;
3142 } else {
3143 item.cxy = pane->positions[i+1] - scroll_pos;
3145 pane->widths_shown[i] = item.cxy;
3146 SendMessageW(pane->hwndHeader, HDM_SETITEMW, i, (LPARAM)&item);
3150 static LRESULT pane_notify(Pane* pane, NMHDR* pnmh)
3152 switch(pnmh->code) {
3153 case HDN_ITEMCHANGEDW: {
3154 LPNMHEADERW phdn = (LPNMHEADERW)pnmh;
3155 int idx = phdn->iItem;
3156 int dx = phdn->pitem->cxy - pane->widths_shown[idx];
3157 int i;
3159 RECT clnt;
3160 GetClientRect(pane->hwnd, &clnt);
3162 pane->widths[idx] += dx;
3163 pane->widths_shown[idx] += dx;
3165 for(i=idx; ++i<=COLUMNS; )
3166 pane->positions[i] += dx;
3169 int scroll_pos = GetScrollPos(pane->hwnd, SB_HORZ);
3170 RECT rt_scr;
3171 RECT rt_clip;
3173 rt_scr.left = pane->positions[idx+1]-scroll_pos;
3174 rt_scr.top = 0;
3175 rt_scr.right = clnt.right;
3176 rt_scr.bottom = clnt.bottom;
3178 rt_clip.left = pane->positions[idx]-scroll_pos;
3179 rt_clip.top = 0;
3180 rt_clip.right = clnt.right;
3181 rt_clip.bottom = clnt.bottom;
3183 if (rt_scr.left < 0) rt_scr.left = 0;
3184 if (rt_clip.left < 0) rt_clip.left = 0;
3186 ScrollWindowEx(pane->hwnd, dx, 0, &rt_scr, &rt_clip, 0, 0, SW_INVALIDATE);
3188 rt_clip.right = pane->positions[idx+1];
3189 RedrawWindow(pane->hwnd, &rt_clip, 0, RDW_INVALIDATE|RDW_UPDATENOW);
3191 if (pnmh->code == HDN_ENDTRACKW) {
3192 SendMessageW(pane->hwnd, LB_SETHORIZONTALEXTENT, pane->positions[COLUMNS], 0);
3194 if (GetScrollPos(pane->hwnd, SB_HORZ) != scroll_pos)
3195 set_header(pane);
3199 return FALSE;
3202 case HDN_DIVIDERDBLCLICKW: {
3203 LPNMHEADERW phdn = (LPNMHEADERW)pnmh;
3204 HDITEMW item;
3206 calc_single_width(pane, phdn->iItem);
3207 item.mask = HDI_WIDTH;
3208 item.cxy = pane->widths[phdn->iItem];
3210 SendMessageW(pane->hwndHeader, HDM_SETITEMW, phdn->iItem, (LPARAM)&item);
3211 InvalidateRect(pane->hwnd, 0, TRUE);
3212 break;}
3215 return 0;
3218 static void scan_entry(ChildWnd* child, Entry* entry, int idx, HWND hwnd)
3220 WCHAR path[MAX_PATH];
3221 HCURSOR old_cursor = SetCursor(LoadCursorW(0, (LPCWSTR)IDC_WAIT));
3223 /* delete sub entries in left pane */
3224 for(;;) {
3225 LRESULT res = SendMessageW(child->left.hwnd, LB_GETITEMDATA, idx+1, 0);
3226 Entry* sub = (Entry*) res;
3228 if (res==LB_ERR || !sub || sub->level<=entry->level)
3229 break;
3231 SendMessageW(child->left.hwnd, LB_DELETESTRING, idx+1, 0);
3234 /* empty right pane */
3235 SendMessageW(child->right.hwnd, LB_RESETCONTENT, 0, 0);
3237 /* release memory */
3238 free_entries(entry);
3240 /* read contents from disk */
3241 if (entry->etype == ET_SHELL)
3243 read_directory(entry, NULL, child->sortOrder, hwnd);
3245 else
3247 get_path(entry, path);
3248 read_directory(entry, path, child->sortOrder, hwnd);
3251 /* insert found entries in right pane */
3252 insert_entries(&child->right, entry->down, child->filter_pattern, child->filter_flags, -1);
3253 calc_widths(&child->right, FALSE);
3254 set_header(&child->right);
3256 child->header_wdths_ok = FALSE;
3258 SetCursor(old_cursor);
3262 /* expand a directory entry */
3264 static BOOL expand_entry(ChildWnd* child, Entry* dir)
3266 int idx;
3267 Entry* p;
3269 if (!dir || dir->expanded || !dir->down)
3270 return FALSE;
3272 p = dir->down;
3274 if (p->data.cFileName[0]=='.' && p->data.cFileName[1]=='\0' && p->next) {
3275 p = p->next;
3277 if (p->data.cFileName[0]=='.' && p->data.cFileName[1]=='.' &&
3278 p->data.cFileName[2]=='\0' && p->next)
3279 p = p->next;
3282 /* no subdirectories ? */
3283 if (!(p->data.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY))
3284 return FALSE;
3286 idx = SendMessageW(child->left.hwnd, LB_FINDSTRING, 0, (LPARAM)dir);
3288 dir->expanded = TRUE;
3290 /* insert entries in left pane */
3291 insert_entries(&child->left, p, NULL, TF_ALL, idx);
3293 if (!child->header_wdths_ok) {
3294 if (calc_widths(&child->left, FALSE)) {
3295 set_header(&child->left);
3297 child->header_wdths_ok = TRUE;
3301 return TRUE;
3305 static void collapse_entry(Pane* pane, Entry* dir)
3307 int idx;
3309 if (!dir) return;
3310 idx = SendMessageW(pane->hwnd, LB_FINDSTRING, 0, (LPARAM)dir);
3312 ShowWindow(pane->hwnd, SW_HIDE);
3314 /* hide sub entries */
3315 for(;;) {
3316 LRESULT res = SendMessageW(pane->hwnd, LB_GETITEMDATA, idx+1, 0);
3317 Entry* sub = (Entry*) res;
3319 if (res==LB_ERR || !sub || sub->level<=dir->level)
3320 break;
3322 SendMessageW(pane->hwnd, LB_DELETESTRING, idx+1, 0);
3325 dir->expanded = FALSE;
3327 ShowWindow(pane->hwnd, SW_SHOW);
3331 static void refresh_right_pane(ChildWnd* child)
3333 SendMessageW(child->right.hwnd, LB_RESETCONTENT, 0, 0);
3334 insert_entries(&child->right, child->right.root, child->filter_pattern, child->filter_flags, -1);
3335 calc_widths(&child->right, FALSE);
3337 set_header(&child->right);
3340 static void set_curdir(ChildWnd* child, Entry* entry, int idx, HWND hwnd)
3342 WCHAR path[MAX_PATH];
3344 if (!entry)
3345 return;
3347 path[0] = '\0';
3349 child->left.cur = entry;
3351 child->right.root = entry->down? entry->down: entry;
3352 child->right.cur = entry;
3354 if (!entry->scanned)
3355 scan_entry(child, entry, idx, hwnd);
3356 else
3357 refresh_right_pane(child);
3359 get_path(entry, path);
3360 lstrcpyW(child->path, path);
3362 if (child->hwnd) /* only change window title, if the window already exists */
3363 SetWindowTextW(child->hwnd, path);
3365 if (path[0])
3366 if (SetCurrentDirectoryW(path))
3367 set_space_status();
3371 static void refresh_child(ChildWnd* child)
3373 WCHAR path[MAX_PATH], drv[_MAX_DRIVE+1];
3374 Entry* entry;
3375 int idx;
3377 get_path(child->left.cur, path);
3378 _wsplitpath(path, drv, NULL, NULL, NULL);
3380 child->right.root = NULL;
3382 scan_entry(child, &child->root.entry, 0, child->hwnd);
3384 if (child->root.entry.etype == ET_SHELL)
3386 LPITEMIDLIST local_pidl = get_path_pidl(path,child->hwnd);
3387 if (local_pidl)
3388 entry = read_tree(&child->root, NULL, local_pidl , drv, child->sortOrder, child->hwnd);
3389 else
3390 entry = NULL;
3392 else
3393 entry = read_tree(&child->root, path, NULL, drv, child->sortOrder, child->hwnd);
3395 if (!entry)
3396 entry = &child->root.entry;
3398 insert_entries(&child->left, child->root.entry.down, NULL, TF_ALL, 0);
3400 set_curdir(child, entry, 0, child->hwnd);
3402 idx = SendMessageW(child->left.hwnd, LB_FINDSTRING, 0, (LPARAM)child->left.cur);
3403 SendMessageW(child->left.hwnd, LB_SETCURSEL, idx, 0);
3407 static void create_drive_bar(void)
3409 TBBUTTON drivebarBtn = {0, 0, TBSTATE_ENABLED, BTNS_BUTTON, {0, 0}, 0, 0};
3410 WCHAR b1[BUFFER_LEN];
3411 int btn = 1;
3412 PWSTR p;
3414 GetLogicalDriveStringsW(BUFFER_LEN, Globals.drives);
3416 Globals.hdrivebar = CreateToolbarEx(Globals.hMainWnd, WS_CHILD|WS_VISIBLE|CCS_NOMOVEY|TBSTYLE_LIST,
3417 IDW_DRIVEBAR, 2, Globals.hInstance, IDB_DRIVEBAR, &drivebarBtn,
3418 0, 16, 13, 16, 13, sizeof(TBBUTTON));
3420 #ifdef __WINE__
3421 /* insert unix file system button */
3422 b1[0] = '/';
3423 b1[1] = '\0';
3424 b1[2] = '\0';
3425 SendMessageW(Globals.hdrivebar, TB_ADDSTRINGW, 0, (LPARAM)b1);
3427 drivebarBtn.idCommand = ID_DRIVE_UNIX_FS;
3428 SendMessageW(Globals.hdrivebar, TB_INSERTBUTTONW, btn++, (LPARAM)&drivebarBtn);
3429 drivebarBtn.iString++;
3430 #endif
3431 /* insert shell namespace button */
3432 load_string(b1, sizeof(b1)/sizeof(b1[0]), IDS_SHELL);
3433 b1[lstrlenW(b1)+1] = '\0';
3434 SendMessageW(Globals.hdrivebar, TB_ADDSTRINGW, 0, (LPARAM)b1);
3436 drivebarBtn.idCommand = ID_DRIVE_SHELL_NS;
3437 SendMessageW(Globals.hdrivebar, TB_INSERTBUTTONW, btn++, (LPARAM)&drivebarBtn);
3438 drivebarBtn.iString++;
3440 /* register windows drive root strings */
3441 SendMessageW(Globals.hdrivebar, TB_ADDSTRINGW, 0, (LPARAM)Globals.drives);
3443 drivebarBtn.idCommand = ID_DRIVE_FIRST;
3445 for(p=Globals.drives; *p; ) {
3446 switch(GetDriveTypeW(p)) {
3447 case DRIVE_REMOVABLE: drivebarBtn.iBitmap = 1; break;
3448 case DRIVE_CDROM: drivebarBtn.iBitmap = 3; break;
3449 case DRIVE_REMOTE: drivebarBtn.iBitmap = 4; break;
3450 case DRIVE_RAMDISK: drivebarBtn.iBitmap = 5; break;
3451 default:/*DRIVE_FIXED*/ drivebarBtn.iBitmap = 2;
3454 SendMessageW(Globals.hdrivebar, TB_INSERTBUTTONW, btn++, (LPARAM)&drivebarBtn);
3455 drivebarBtn.idCommand++;
3456 drivebarBtn.iString++;
3458 while(*p++);
3462 static void refresh_drives(void)
3464 RECT rect;
3466 /* destroy drive bar */
3467 DestroyWindow(Globals.hdrivebar);
3468 Globals.hdrivebar = 0;
3470 /* re-create drive bar */
3471 create_drive_bar();
3473 /* update window layout */
3474 GetClientRect(Globals.hMainWnd, &rect);
3475 SendMessageW(Globals.hMainWnd, WM_SIZE, 0, MAKELONG(rect.right, rect.bottom));
3479 static BOOL launch_file(HWND hwnd, LPCWSTR cmd, UINT nCmdShow)
3481 HINSTANCE hinst = ShellExecuteW(hwnd, NULL/*operation*/, cmd, NULL/*parameters*/, NULL/*dir*/, nCmdShow);
3483 if (PtrToUlong(hinst) <= 32) {
3484 display_error(hwnd, GetLastError());
3485 return FALSE;
3488 return TRUE;
3492 static BOOL launch_entry(Entry* entry, HWND hwnd, UINT nCmdShow)
3494 WCHAR cmd[MAX_PATH];
3496 if (entry->etype == ET_SHELL) {
3497 BOOL ret = TRUE;
3499 SHELLEXECUTEINFOW shexinfo;
3501 shexinfo.cbSize = sizeof(SHELLEXECUTEINFOW);
3502 shexinfo.fMask = SEE_MASK_IDLIST;
3503 shexinfo.hwnd = hwnd;
3504 shexinfo.lpVerb = NULL;
3505 shexinfo.lpFile = NULL;
3506 shexinfo.lpParameters = NULL;
3507 shexinfo.lpDirectory = NULL;
3508 shexinfo.nShow = nCmdShow;
3509 shexinfo.lpIDList = get_to_absolute_pidl(entry, hwnd);
3511 if (!ShellExecuteExW(&shexinfo)) {
3512 display_error(hwnd, GetLastError());
3513 ret = FALSE;
3516 if (shexinfo.lpIDList != entry->pidl)
3517 IMalloc_Free(Globals.iMalloc, shexinfo.lpIDList);
3519 return ret;
3522 get_path(entry, cmd);
3524 /* start program, open document... */
3525 return launch_file(hwnd, cmd, nCmdShow);
3529 static void activate_entry(ChildWnd* child, Pane* pane, HWND hwnd)
3531 Entry* entry = pane->cur;
3533 if (!entry)
3534 return;
3536 if (entry->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
3537 int scanned_old = entry->scanned;
3539 if (!scanned_old)
3541 int idx = SendMessageW(child->left.hwnd, LB_GETCURSEL, 0, 0);
3542 scan_entry(child, entry, idx, hwnd);
3545 if (entry->data.cFileName[0]=='.' && entry->data.cFileName[1]=='\0')
3546 return;
3548 if (entry->data.cFileName[0]=='.' && entry->data.cFileName[1]=='.' && entry->data.cFileName[2]=='\0') {
3549 entry = child->left.cur->up;
3550 collapse_entry(&child->left, entry);
3551 goto focus_entry;
3552 } else if (entry->expanded)
3553 collapse_entry(pane, child->left.cur);
3554 else {
3555 expand_entry(child, child->left.cur);
3557 if (!pane->treePane) focus_entry: {
3558 int idxstart = SendMessageW(child->left.hwnd, LB_GETCURSEL, 0, 0);
3559 int idx = SendMessageW(child->left.hwnd, LB_FINDSTRING, idxstart, (LPARAM)entry);
3560 SendMessageW(child->left.hwnd, LB_SETCURSEL, idx, 0);
3561 set_curdir(child, entry, idx, hwnd);
3565 if (!scanned_old) {
3566 calc_widths(pane, FALSE);
3568 set_header(pane);
3570 } else {
3571 if (GetKeyState(VK_MENU) < 0)
3572 show_properties_dlg(entry, child->hwnd);
3573 else
3574 launch_entry(entry, child->hwnd, SW_SHOWNORMAL);
3579 static BOOL pane_command(Pane* pane, UINT cmd)
3581 switch(cmd) {
3582 case ID_VIEW_NAME:
3583 if (pane->visible_cols) {
3584 pane->visible_cols = 0;
3585 calc_widths(pane, TRUE);
3586 set_header(pane);
3587 InvalidateRect(pane->hwnd, 0, TRUE);
3588 CheckMenuItem(Globals.hMenuView, ID_VIEW_NAME, MF_BYCOMMAND|MF_CHECKED);
3589 CheckMenuItem(Globals.hMenuView, ID_VIEW_ALL_ATTRIBUTES, MF_BYCOMMAND);
3591 break;
3593 case ID_VIEW_ALL_ATTRIBUTES:
3594 if (pane->visible_cols != COL_ALL) {
3595 pane->visible_cols = COL_ALL;
3596 calc_widths(pane, TRUE);
3597 set_header(pane);
3598 InvalidateRect(pane->hwnd, 0, TRUE);
3599 CheckMenuItem(Globals.hMenuView, ID_VIEW_NAME, MF_BYCOMMAND);
3600 CheckMenuItem(Globals.hMenuView, ID_VIEW_ALL_ATTRIBUTES, MF_BYCOMMAND|MF_CHECKED);
3602 break;
3604 case ID_PREFERRED_SIZES: {
3605 calc_widths(pane, TRUE);
3606 set_header(pane);
3607 InvalidateRect(pane->hwnd, 0, TRUE);
3608 break;}
3610 /* TODO: more command ids... */
3612 default:
3613 return FALSE;
3616 return TRUE;
3620 static void set_sort_order(ChildWnd* child, SORT_ORDER sortOrder)
3622 if (child->sortOrder != sortOrder) {
3623 child->sortOrder = sortOrder;
3624 refresh_child(child);
3628 static void update_view_menu(ChildWnd* child)
3630 CheckMenuItem(Globals.hMenuView, ID_VIEW_SORT_NAME, child->sortOrder==SORT_NAME? MF_CHECKED: MF_UNCHECKED);
3631 CheckMenuItem(Globals.hMenuView, ID_VIEW_SORT_TYPE, child->sortOrder==SORT_EXT? MF_CHECKED: MF_UNCHECKED);
3632 CheckMenuItem(Globals.hMenuView, ID_VIEW_SORT_SIZE, child->sortOrder==SORT_SIZE? MF_CHECKED: MF_UNCHECKED);
3633 CheckMenuItem(Globals.hMenuView, ID_VIEW_SORT_DATE, child->sortOrder==SORT_DATE? MF_CHECKED: MF_UNCHECKED);
3637 static BOOL is_directory(LPCWSTR target)
3639 /*TODO correctly handle UNIX paths */
3640 DWORD target_attr = GetFileAttributesW(target);
3642 if (target_attr == INVALID_FILE_ATTRIBUTES)
3643 return FALSE;
3645 return (target_attr & FILE_ATTRIBUTE_DIRECTORY) != 0;
3648 static BOOL prompt_target(Pane* pane, LPWSTR source, LPWSTR target)
3650 WCHAR path[MAX_PATH];
3651 int len;
3653 get_path(pane->cur, path);
3655 if (DialogBoxParamW(Globals.hInstance, MAKEINTRESOURCEW(IDD_SELECT_DESTINATION), pane->hwnd, DestinationDlgProc, (LPARAM)path) != IDOK)
3656 return FALSE;
3658 get_path(pane->cur, source);
3660 /* convert relative targets to absolute paths */
3661 if (path[0]!='/' && path[1]!=':') {
3662 get_path(pane->cur->up, target);
3663 len = lstrlenW(target);
3665 if (target[len-1]!='\\' && target[len-1]!='/')
3666 target[len++] = '/';
3668 lstrcpyW(target+len, path);
3669 } else
3670 lstrcpyW(target, path);
3672 /* If the target already exists as directory, create a new target below this. */
3673 if (is_directory(path)) {
3674 WCHAR fname[_MAX_FNAME], ext[_MAX_EXT];
3675 static const WCHAR sAppend[] = {'%','s','/','%','s','%','s','\0'};
3677 _wsplitpath(source, NULL, NULL, fname, ext);
3679 wsprintfW(target, sAppend, path, fname, ext);
3682 return TRUE;
3686 static IContextMenu2* s_pctxmenu2 = NULL;
3687 static IContextMenu3* s_pctxmenu3 = NULL;
3689 static void CtxMenu_reset(void)
3691 s_pctxmenu2 = NULL;
3692 s_pctxmenu3 = NULL;
3695 static IContextMenu* CtxMenu_query_interfaces(IContextMenu* pcm1)
3697 IContextMenu* pcm = NULL;
3699 CtxMenu_reset();
3701 if (IContextMenu_QueryInterface(pcm1, &IID_IContextMenu3, (void**)&pcm) == NOERROR)
3702 s_pctxmenu3 = (LPCONTEXTMENU3)pcm;
3703 else if (IContextMenu_QueryInterface(pcm1, &IID_IContextMenu2, (void**)&pcm) == NOERROR)
3704 s_pctxmenu2 = (LPCONTEXTMENU2)pcm;
3706 if (pcm) {
3707 IContextMenu_Release(pcm1);
3708 return pcm;
3709 } else
3710 return pcm1;
3713 static BOOL CtxMenu_HandleMenuMsg(UINT nmsg, WPARAM wparam, LPARAM lparam)
3715 if (s_pctxmenu3) {
3716 if (SUCCEEDED(IContextMenu3_HandleMenuMsg(s_pctxmenu3, nmsg, wparam, lparam)))
3717 return TRUE;
3720 if (s_pctxmenu2)
3721 if (SUCCEEDED(IContextMenu2_HandleMenuMsg(s_pctxmenu2, nmsg, wparam, lparam)))
3722 return TRUE;
3724 return FALSE;
3727 static HRESULT ShellFolderContextMenu(IShellFolder* shell_folder, HWND hwndParent, int cidl, LPCITEMIDLIST* apidl, int x, int y)
3729 IContextMenu* pcm;
3730 BOOL executed = FALSE;
3732 HRESULT hr = IShellFolder_GetUIObjectOf(shell_folder, hwndParent, cidl, apidl, &IID_IContextMenu, NULL, (LPVOID*)&pcm);
3734 if (SUCCEEDED(hr)) {
3735 HMENU hmenu = CreatePopupMenu();
3737 pcm = CtxMenu_query_interfaces(pcm);
3739 if (hmenu) {
3740 hr = IContextMenu_QueryContextMenu(pcm, hmenu, 0, FCIDM_SHVIEWFIRST, FCIDM_SHVIEWLAST, CMF_NORMAL);
3742 if (SUCCEEDED(hr)) {
3743 UINT idCmd = TrackPopupMenu(hmenu, TPM_LEFTALIGN|TPM_RETURNCMD|TPM_RIGHTBUTTON, x, y, 0, hwndParent, NULL);
3745 CtxMenu_reset();
3747 if (idCmd) {
3748 CMINVOKECOMMANDINFO cmi;
3750 cmi.cbSize = sizeof(CMINVOKECOMMANDINFO);
3751 cmi.fMask = 0;
3752 cmi.hwnd = hwndParent;
3753 cmi.lpVerb = (LPCSTR)(INT_PTR)(idCmd - FCIDM_SHVIEWFIRST);
3754 cmi.lpParameters = NULL;
3755 cmi.lpDirectory = NULL;
3756 cmi.nShow = SW_SHOWNORMAL;
3757 cmi.dwHotKey = 0;
3758 cmi.hIcon = 0;
3760 hr = IContextMenu_InvokeCommand(pcm, &cmi);
3761 executed = TRUE;
3763 } else
3764 CtxMenu_reset();
3767 IContextMenu_Release(pcm);
3770 return FAILED(hr)? hr: executed? S_OK: S_FALSE;
3773 static LRESULT CALLBACK ChildWndProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam)
3775 ChildWnd* child = (ChildWnd*)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
3776 ASSERT(child);
3778 switch(nmsg) {
3779 case WM_DRAWITEM: {
3780 LPDRAWITEMSTRUCT dis = (LPDRAWITEMSTRUCT)lparam;
3781 Entry* entry = (Entry*) dis->itemData;
3783 if (dis->CtlID == IDW_TREE_LEFT)
3784 draw_item(&child->left, dis, entry, -1);
3785 else if (dis->CtlID == IDW_TREE_RIGHT)
3786 draw_item(&child->right, dis, entry, -1);
3787 else
3788 goto draw_menu_item;
3790 return TRUE;}
3792 case WM_CREATE:
3793 InitChildWindow(child);
3794 break;
3796 case WM_NCDESTROY:
3797 free_child_window(child);
3798 SetWindowLongPtrW(hwnd, GWLP_USERDATA, 0);
3799 break;
3801 case WM_PAINT: {
3802 PAINTSTRUCT ps;
3803 HBRUSH lastBrush;
3804 RECT rt;
3805 GetClientRect(hwnd, &rt);
3806 BeginPaint(hwnd, &ps);
3807 rt.left = child->split_pos-SPLIT_WIDTH/2;
3808 rt.right = child->split_pos+SPLIT_WIDTH/2+1;
3809 lastBrush = SelectObject(ps.hdc, GetStockObject(COLOR_SPLITBAR));
3810 Rectangle(ps.hdc, rt.left, rt.top-1, rt.right, rt.bottom+1);
3811 SelectObject(ps.hdc, lastBrush);
3812 EndPaint(hwnd, &ps);
3813 break;}
3815 case WM_SETCURSOR:
3816 if (LOWORD(lparam) == HTCLIENT) {
3817 POINT pt;
3818 GetCursorPos(&pt);
3819 ScreenToClient(hwnd, &pt);
3821 if (pt.x>=child->split_pos-SPLIT_WIDTH/2 && pt.x<child->split_pos+SPLIT_WIDTH/2+1) {
3822 SetCursor(LoadCursorW(0, (LPCWSTR)IDC_SIZEWE));
3823 return TRUE;
3826 goto def;
3828 case WM_LBUTTONDOWN: {
3829 RECT rt;
3830 int x = (short)LOWORD(lparam);
3832 GetClientRect(hwnd, &rt);
3834 if (x>=child->split_pos-SPLIT_WIDTH/2 && x<child->split_pos+SPLIT_WIDTH/2+1) {
3835 last_split = child->split_pos;
3836 SetCapture(hwnd);
3839 break;}
3841 case WM_LBUTTONUP:
3842 if (GetCapture() == hwnd)
3843 ReleaseCapture();
3844 break;
3846 case WM_KEYDOWN:
3847 if (wparam == VK_ESCAPE)
3848 if (GetCapture() == hwnd) {
3849 RECT rt;
3850 child->split_pos = last_split;
3851 GetClientRect(hwnd, &rt);
3852 resize_tree(child, rt.right, rt.bottom);
3853 last_split = -1;
3854 ReleaseCapture();
3855 SetCursor(LoadCursorW(0, (LPCWSTR)IDC_ARROW));
3857 break;
3859 case WM_MOUSEMOVE:
3860 if (GetCapture() == hwnd) {
3861 RECT rt;
3862 int x = (short)LOWORD(lparam);
3864 GetClientRect(hwnd, &rt);
3866 if (x>=0 && x<rt.right) {
3867 child->split_pos = x;
3868 resize_tree(child, rt.right, rt.bottom);
3869 rt.left = x-SPLIT_WIDTH/2;
3870 rt.right = x+SPLIT_WIDTH/2+1;
3871 InvalidateRect(hwnd, &rt, FALSE);
3872 UpdateWindow(child->left.hwnd);
3873 UpdateWindow(hwnd);
3874 UpdateWindow(child->right.hwnd);
3877 break;
3879 case WM_GETMINMAXINFO:
3880 DefMDIChildProcW(hwnd, nmsg, wparam, lparam);
3882 {LPMINMAXINFO lpmmi = (LPMINMAXINFO)lparam;
3884 lpmmi->ptMaxTrackSize.x <<= 1;/*2*GetSystemMetrics(SM_CXSCREEN) / SM_CXVIRTUALSCREEN */
3885 lpmmi->ptMaxTrackSize.y <<= 1;/*2*GetSystemMetrics(SM_CYSCREEN) / SM_CYVIRTUALSCREEN */
3886 break;}
3888 case WM_SETFOCUS:
3889 if (SetCurrentDirectoryW(child->path))
3890 set_space_status();
3891 SetFocus(child->focus_pane? child->right.hwnd: child->left.hwnd);
3892 break;
3894 case WM_DISPATCH_COMMAND: {
3895 Pane* pane = GetFocus()==child->left.hwnd? &child->left: &child->right;
3897 switch(LOWORD(wparam)) {
3898 case ID_WINDOW_NEW: {
3899 ChildWnd* new_child = alloc_child_window(child->path, NULL, hwnd);
3901 if (!create_child_window(new_child))
3902 HeapFree(GetProcessHeap(), 0, new_child);
3904 break;}
3906 case ID_REFRESH:
3907 refresh_drives();
3908 refresh_child(child);
3909 break;
3911 case ID_ACTIVATE:
3912 activate_entry(child, pane, hwnd);
3913 break;
3915 case ID_FILE_MOVE: {
3916 WCHAR source[BUFFER_LEN], target[BUFFER_LEN];
3918 if (prompt_target(pane, source, target)) {
3919 SHFILEOPSTRUCTW shfo = {hwnd, FO_MOVE, source, target};
3921 source[lstrlenW(source)+1] = '\0';
3922 target[lstrlenW(target)+1] = '\0';
3924 if (!SHFileOperationW(&shfo))
3925 refresh_child(child);
3927 break;}
3929 case ID_FILE_COPY: {
3930 WCHAR source[BUFFER_LEN], target[BUFFER_LEN];
3932 if (prompt_target(pane, source, target)) {
3933 SHFILEOPSTRUCTW shfo = {hwnd, FO_COPY, source, target};
3935 source[lstrlenW(source)+1] = '\0';
3936 target[lstrlenW(target)+1] = '\0';
3938 if (!SHFileOperationW(&shfo))
3939 refresh_child(child);
3941 break;}
3943 case ID_FILE_DELETE: {
3944 WCHAR path[BUFFER_LEN];
3945 SHFILEOPSTRUCTW shfo = {hwnd, FO_DELETE, path, NULL, FOF_ALLOWUNDO};
3947 get_path(pane->cur, path);
3949 path[lstrlenW(path)+1] = '\0';
3951 if (!SHFileOperationW(&shfo))
3952 refresh_child(child);
3953 break;}
3955 case ID_VIEW_SORT_NAME:
3956 set_sort_order(child, SORT_NAME);
3957 break;
3959 case ID_VIEW_SORT_TYPE:
3960 set_sort_order(child, SORT_EXT);
3961 break;
3963 case ID_VIEW_SORT_SIZE:
3964 set_sort_order(child, SORT_SIZE);
3965 break;
3967 case ID_VIEW_SORT_DATE:
3968 set_sort_order(child, SORT_DATE);
3969 break;
3971 case ID_VIEW_FILTER: {
3972 struct FilterDialog dlg;
3974 memset(&dlg, 0, sizeof(struct FilterDialog));
3975 lstrcpyW(dlg.pattern, child->filter_pattern);
3976 dlg.flags = child->filter_flags;
3978 if (DialogBoxParamW(Globals.hInstance, MAKEINTRESOURCEW(IDD_DIALOG_VIEW_TYPE), hwnd, FilterDialogDlgProc, (LPARAM)&dlg) == IDOK) {
3979 lstrcpyW(child->filter_pattern, dlg.pattern);
3980 child->filter_flags = dlg.flags;
3981 refresh_right_pane(child);
3983 break;}
3985 case ID_VIEW_SPLIT: {
3986 last_split = child->split_pos;
3987 SetCapture(hwnd);
3988 break;}
3990 case ID_EDIT_PROPERTIES:
3991 show_properties_dlg(pane->cur, child->hwnd);
3992 break;
3994 default:
3995 return pane_command(pane, LOWORD(wparam));
3998 return TRUE;}
4000 case WM_COMMAND: {
4001 Pane* pane = GetFocus()==child->left.hwnd? &child->left: &child->right;
4003 switch(HIWORD(wparam)) {
4004 case LBN_SELCHANGE: {
4005 int idx = SendMessageW(pane->hwnd, LB_GETCURSEL, 0, 0);
4006 Entry* entry = (Entry*)SendMessageW(pane->hwnd, LB_GETITEMDATA, idx, 0);
4008 if (pane == &child->left)
4009 set_curdir(child, entry, idx, hwnd);
4010 else
4011 pane->cur = entry;
4012 break;}
4014 case LBN_DBLCLK:
4015 activate_entry(child, pane, hwnd);
4016 break;
4018 break;}
4020 case WM_NOTIFY: {
4021 NMHDR* pnmh = (NMHDR*) lparam;
4022 return pane_notify(pnmh->idFrom==IDW_HEADER_LEFT? &child->left: &child->right, pnmh);}
4024 case WM_CONTEXTMENU: {
4025 POINT pt, pt_clnt;
4026 Pane* pane;
4027 int idx;
4029 /* first select the current item in the listbox */
4030 HWND hpanel = (HWND) wparam;
4031 pt_clnt.x = pt.x = (short)LOWORD(lparam);
4032 pt_clnt.y = pt.y = (short)HIWORD(lparam);
4033 ScreenToClient(hpanel, &pt_clnt);
4034 SendMessageW(hpanel, WM_LBUTTONDOWN, 0, MAKELONG(pt_clnt.x, pt_clnt.y));
4035 SendMessageW(hpanel, WM_LBUTTONUP, 0, MAKELONG(pt_clnt.x, pt_clnt.y));
4037 /* now create the popup menu using shell namespace and IContextMenu */
4038 pane = GetFocus()==child->left.hwnd? &child->left: &child->right;
4039 idx = SendMessageW(pane->hwnd, LB_GETCURSEL, 0, 0);
4041 if (idx != -1) {
4042 Entry* entry = (Entry*)SendMessageW(pane->hwnd, LB_GETITEMDATA, idx, 0);
4044 LPITEMIDLIST pidl_abs = get_to_absolute_pidl(entry, hwnd);
4046 if (pidl_abs) {
4047 IShellFolder* parentFolder;
4048 LPCITEMIDLIST pidlLast;
4050 /* get and use the parent folder to display correct context menu in all cases */
4051 if (SUCCEEDED(SHBindToParent(pidl_abs, &IID_IShellFolder, (LPVOID*)&parentFolder, &pidlLast))) {
4052 if (ShellFolderContextMenu(parentFolder, hwnd, 1, &pidlLast, pt.x, pt.y) == S_OK)
4053 refresh_child(child);
4055 IShellFolder_Release(parentFolder);
4058 IMalloc_Free(Globals.iMalloc, pidl_abs);
4061 break;}
4063 case WM_MEASUREITEM:
4064 draw_menu_item:
4065 if (!wparam) /* Is the message menu-related? */
4066 if (CtxMenu_HandleMenuMsg(nmsg, wparam, lparam))
4067 return TRUE;
4069 break;
4071 case WM_INITMENUPOPUP:
4072 if (CtxMenu_HandleMenuMsg(nmsg, wparam, lparam))
4073 return 0;
4075 update_view_menu(child);
4076 break;
4078 case WM_MENUCHAR: /* only supported by IContextMenu3 */
4079 if (s_pctxmenu3) {
4080 LRESULT lResult = 0;
4082 IContextMenu3_HandleMenuMsg2(s_pctxmenu3, nmsg, wparam, lparam, &lResult);
4084 return lResult;
4087 break;
4089 case WM_SIZE:
4090 if (wparam != SIZE_MINIMIZED)
4091 resize_tree(child, LOWORD(lparam), HIWORD(lparam));
4092 /* fall through */
4094 default: def:
4095 return DefMDIChildProcW(hwnd, nmsg, wparam, lparam);
4098 return 0;
4102 static LRESULT CALLBACK TreeWndProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam)
4104 ChildWnd* child = (ChildWnd*)GetWindowLongPtrW(GetParent(hwnd), GWLP_USERDATA);
4105 Pane* pane = (Pane*)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
4106 ASSERT(child);
4108 switch(nmsg) {
4109 case WM_HSCROLL:
4110 set_header(pane);
4111 break;
4113 case WM_SETFOCUS:
4114 child->focus_pane = pane==&child->right? 1: 0;
4115 SendMessageW(hwnd, LB_SETSEL, TRUE, 1);
4116 /*TODO: check menu items */
4117 break;
4119 case WM_KEYDOWN:
4120 if (wparam == VK_TAB) {
4121 /*TODO: SetFocus(Globals.hdrivebar) */
4122 SetFocus(child->focus_pane? child->left.hwnd: child->right.hwnd);
4126 return CallWindowProcW(g_orgTreeWndProc, hwnd, nmsg, wparam, lparam);
4130 static void InitInstance(HINSTANCE hinstance)
4132 static const WCHAR sFont[] = {'M','i','c','r','o','s','o','f','t',' ','S','a','n','s',' ','S','e','r','i','f','\0'};
4134 WNDCLASSEXW wcFrame;
4135 WNDCLASSW wcChild;
4136 int col;
4138 INITCOMMONCONTROLSEX icc = {
4139 sizeof(INITCOMMONCONTROLSEX),
4140 ICC_BAR_CLASSES
4143 HDC hdc = GetDC(0);
4145 setlocale(LC_COLLATE, ""); /* set collating rules to local settings for compareName */
4147 InitCommonControlsEx(&icc);
4150 /* register frame window class */
4152 wcFrame.cbSize = sizeof(WNDCLASSEXW);
4153 wcFrame.style = 0;
4154 wcFrame.lpfnWndProc = FrameWndProc;
4155 wcFrame.cbClsExtra = 0;
4156 wcFrame.cbWndExtra = 0;
4157 wcFrame.hInstance = hinstance;
4158 wcFrame.hIcon = LoadIconW(hinstance, MAKEINTRESOURCEW(IDI_WINEFILE));
4159 wcFrame.hCursor = LoadCursorW(0, (LPCWSTR)IDC_ARROW);
4160 wcFrame.hbrBackground = 0;
4161 wcFrame.lpszMenuName = 0;
4162 wcFrame.lpszClassName = sWINEFILEFRAME;
4163 wcFrame.hIconSm = LoadImageW(hinstance, MAKEINTRESOURCEW(IDI_WINEFILE), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED);
4165 Globals.hframeClass = RegisterClassExW(&wcFrame);
4168 /* register tree windows class */
4170 wcChild.style = CS_CLASSDC|CS_DBLCLKS|CS_VREDRAW;
4171 wcChild.lpfnWndProc = ChildWndProc;
4172 wcChild.cbClsExtra = 0;
4173 wcChild.cbWndExtra = 0;
4174 wcChild.hInstance = hinstance;
4175 wcChild.hIcon = LoadIconW(hinstance, MAKEINTRESOURCEW(IDI_WINEFILE));
4176 wcChild.hCursor = LoadCursorW(0, (LPCWSTR)IDC_ARROW);
4177 wcChild.hbrBackground = 0;
4178 wcChild.lpszMenuName = 0;
4179 wcChild.lpszClassName = sWINEFILETREE;
4181 RegisterClassW(&wcChild);
4184 Globals.haccel = LoadAcceleratorsW(hinstance, MAKEINTRESOURCEW(IDA_WINEFILE));
4186 Globals.hfont = CreateFontW(-MulDiv(8,GetDeviceCaps(hdc,LOGPIXELSY),72), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, sFont);
4188 ReleaseDC(0, hdc);
4190 Globals.hInstance = hinstance;
4192 CoInitialize(NULL);
4193 CoGetMalloc(MEMCTX_TASK, &Globals.iMalloc);
4194 SHGetDesktopFolder(&Globals.iDesktop);
4195 Globals.cfStrFName = RegisterClipboardFormatW(CFSTR_FILENAMEW);
4197 /* load column strings */
4198 col = 1;
4200 load_string(g_pos_names[col++], sizeof(g_pos_names[col])/sizeof(g_pos_names[col][0]), IDS_COL_NAME);
4201 load_string(g_pos_names[col++], sizeof(g_pos_names[col])/sizeof(g_pos_names[col][0]), IDS_COL_SIZE);
4202 load_string(g_pos_names[col++], sizeof(g_pos_names[col])/sizeof(g_pos_names[col][0]), IDS_COL_CDATE);
4203 load_string(g_pos_names[col++], sizeof(g_pos_names[col])/sizeof(g_pos_names[col][0]), IDS_COL_ADATE);
4204 load_string(g_pos_names[col++], sizeof(g_pos_names[col])/sizeof(g_pos_names[col][0]), IDS_COL_MDATE);
4205 load_string(g_pos_names[col++], sizeof(g_pos_names[col])/sizeof(g_pos_names[col][0]), IDS_COL_IDX);
4206 load_string(g_pos_names[col++], sizeof(g_pos_names[col])/sizeof(g_pos_names[col][0]), IDS_COL_LINKS);
4207 load_string(g_pos_names[col++], sizeof(g_pos_names[col])/sizeof(g_pos_names[col][0]), IDS_COL_ATTR);
4208 load_string(g_pos_names[col++], sizeof(g_pos_names[col])/sizeof(g_pos_names[col][0]), IDS_COL_SEC);
4212 static BOOL show_frame(HWND hwndParent, int cmdshow, LPCWSTR path)
4214 static const WCHAR sMDICLIENT[] = {'M','D','I','C','L','I','E','N','T','\0'};
4216 WCHAR buffer[MAX_PATH], b1[BUFFER_LEN];
4217 ChildWnd* child;
4218 HMENU hMenuFrame, hMenuWindow;
4219 windowOptions opts;
4221 CLIENTCREATESTRUCT ccs;
4223 if (Globals.hMainWnd)
4224 return TRUE;
4226 opts = load_registry_settings();
4227 hMenuFrame = LoadMenuW(Globals.hInstance, MAKEINTRESOURCEW(IDM_WINEFILE));
4228 hMenuWindow = GetSubMenu(hMenuFrame, GetMenuItemCount(hMenuFrame)-2);
4230 Globals.hMenuFrame = hMenuFrame;
4231 Globals.hMenuView = GetSubMenu(hMenuFrame, 2);
4232 Globals.hMenuOptions = GetSubMenu(hMenuFrame, 3);
4234 ccs.hWindowMenu = hMenuWindow;
4235 ccs.idFirstChild = IDW_FIRST_CHILD;
4238 /* create main window */
4239 Globals.hMainWnd = CreateWindowExW(0, MAKEINTRESOURCEW(Globals.hframeClass), RS(b1,IDS_WINEFILE), WS_OVERLAPPEDWINDOW,
4240 opts.start_x, opts.start_y, opts.width, opts.height,
4241 hwndParent, Globals.hMenuFrame, Globals.hInstance, 0/*lpParam*/);
4244 Globals.hmdiclient = CreateWindowExW(0, sMDICLIENT, NULL,
4245 WS_CHILD|WS_CLIPCHILDREN|WS_VSCROLL|WS_HSCROLL|WS_VISIBLE|WS_BORDER,
4246 0, 0, 0, 0,
4247 Globals.hMainWnd, 0, Globals.hInstance, &ccs);
4249 CheckMenuItem(Globals.hMenuOptions, ID_VIEW_DRIVE_BAR, MF_BYCOMMAND|MF_CHECKED);
4250 CheckMenuItem(Globals.hMenuOptions, ID_VIEW_SAVESETTINGS, MF_BYCOMMAND);
4252 create_drive_bar();
4255 TBBUTTON toolbarBtns[] = {
4256 {0, 0, 0, BTNS_SEP, {0, 0}, 0, 0},
4257 {0, ID_WINDOW_NEW, TBSTATE_ENABLED, BTNS_BUTTON, {0, 0}, 0, 0},
4258 {1, ID_WINDOW_CASCADE, TBSTATE_ENABLED, BTNS_BUTTON, {0, 0}, 0, 0},
4259 {2, ID_WINDOW_TILE_HORZ, TBSTATE_ENABLED, BTNS_BUTTON, {0, 0}, 0, 0},
4260 {3, ID_WINDOW_TILE_VERT, TBSTATE_ENABLED, BTNS_BUTTON, {0, 0}, 0, 0},
4263 Globals.htoolbar = CreateToolbarEx(Globals.hMainWnd, WS_CHILD|WS_VISIBLE,
4264 IDW_TOOLBAR, 2, Globals.hInstance, IDB_TOOLBAR, toolbarBtns,
4265 sizeof(toolbarBtns)/sizeof(TBBUTTON), 16, 15, 16, 15, sizeof(TBBUTTON));
4266 CheckMenuItem(Globals.hMenuOptions, ID_VIEW_TOOL_BAR, MF_BYCOMMAND|MF_CHECKED);
4269 Globals.hstatusbar = CreateStatusWindowW(WS_CHILD|WS_VISIBLE, 0, Globals.hMainWnd, IDW_STATUSBAR);
4270 CheckMenuItem(Globals.hMenuOptions, ID_VIEW_STATUSBAR, MF_BYCOMMAND|MF_CHECKED);
4272 /*TODO: read paths from registry */
4274 if (!path || !*path) {
4275 GetCurrentDirectoryW(MAX_PATH, buffer);
4276 path = buffer;
4279 ShowWindow(Globals.hMainWnd, cmdshow);
4281 #ifndef __WINE__
4282 /* Shell Namespace as default: */
4283 child = alloc_child_window(path, get_path_pidl(path,Globals.hMainWnd), Globals.hMainWnd);
4284 #else
4285 child = alloc_child_window(path, NULL, Globals.hMainWnd);
4286 #endif
4288 child->pos.showCmd = SW_SHOWMAXIMIZED;
4289 child->pos.rcNormalPosition.left = 0;
4290 child->pos.rcNormalPosition.top = 0;
4291 child->pos.rcNormalPosition.right = 320;
4292 child->pos.rcNormalPosition.bottom = 280;
4294 if (!create_child_window(child)) {
4295 HeapFree(GetProcessHeap(), 0, child);
4296 return FALSE;
4299 SetWindowPlacement(child->hwnd, &child->pos);
4301 Globals.himl = ImageList_LoadImageW(Globals.hInstance, MAKEINTRESOURCEW(IDB_IMAGES), 16, 0, RGB(0,255,0), IMAGE_BITMAP, 0);
4303 Globals.prescan_node = FALSE;
4305 UpdateWindow(Globals.hMainWnd);
4307 if (child->hwnd && path && path[0])
4309 int index,count;
4310 WCHAR drv[_MAX_DRIVE+1], dir[_MAX_DIR], name[_MAX_FNAME], ext[_MAX_EXT];
4311 WCHAR fullname[_MAX_FNAME+_MAX_EXT+1];
4313 memset(name,0,sizeof(name));
4314 memset(name,0,sizeof(ext));
4315 _wsplitpath(path, drv, dir, name, ext);
4316 if (name[0])
4318 count = SendMessageW(child->right.hwnd, LB_GETCOUNT, 0, 0);
4319 lstrcpyW(fullname,name);
4320 lstrcatW(fullname,ext);
4322 for (index = 0; index < count; index ++)
4324 Entry* entry = (Entry*)SendMessageW(child->right.hwnd, LB_GETITEMDATA, index, 0);
4325 if (lstrcmpW(entry->data.cFileName,fullname)==0 ||
4326 lstrcmpW(entry->data.cAlternateFileName,fullname)==0)
4328 SendMessageW(child->right.hwnd, LB_SETCURSEL, index, 0);
4329 SetFocus(child->right.hwnd);
4330 break;
4335 return TRUE;
4338 static void ExitInstance(void)
4340 IShellFolder_Release(Globals.iDesktop);
4341 IMalloc_Release(Globals.iMalloc);
4342 CoUninitialize();
4344 DeleteObject(Globals.hfont);
4345 ImageList_Destroy(Globals.himl);
4348 int APIENTRY wWinMain(HINSTANCE hinstance, HINSTANCE previnstance, LPWSTR cmdline, int cmdshow)
4350 MSG msg;
4352 InitInstance(hinstance);
4354 if( !show_frame(0, cmdshow, cmdline) )
4356 ExitInstance();
4357 return 1;
4360 while(GetMessageW(&msg, 0, 0, 0)) {
4361 if (Globals.hmdiclient && TranslateMDISysAccel(Globals.hmdiclient, &msg))
4362 continue;
4364 if (Globals.hMainWnd && TranslateAcceleratorW(Globals.hMainWnd, Globals.haccel, &msg))
4365 continue;
4367 TranslateMessage(&msg);
4368 DispatchMessageW(&msg);
4371 ExitInstance();
4373 return msg.wParam;