gdi32: Clip the computed region to the DIB rectangle in PolyPolygon().
[wine.git] / programs / winefile / winefile.c
blob044a399a82ffba8cff009d42422163cbc30af2ba
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 wp.length = sizeof(wp);
1927 GetWindowPlacement(hwnd, &wp);
1929 prect->left = prect->top = 0;
1930 prect->right = wp.rcNormalPosition.right-wp.rcNormalPosition.left-
1931 2*(GetSystemMetrics(SM_CXSIZEFRAME)+GetSystemMetrics(SM_CXEDGE));
1932 prect->bottom = wp.rcNormalPosition.bottom-wp.rcNormalPosition.top-
1933 2*(GetSystemMetrics(SM_CYSIZEFRAME)+GetSystemMetrics(SM_CYEDGE))-
1934 GetSystemMetrics(SM_CYCAPTION)-GetSystemMetrics(SM_CYMENUSIZE);
1937 if (IsWindowVisible(Globals.htoolbar)) {
1938 GetClientRect(Globals.htoolbar, &rt);
1939 prect->top += rt.bottom+2;
1942 if (IsWindowVisible(Globals.hdrivebar)) {
1943 GetClientRect(Globals.hdrivebar, &rt);
1944 prect->top += rt.bottom+2;
1947 if (IsWindowVisible(Globals.hstatusbar)) {
1948 GetClientRect(Globals.hstatusbar, &rt);
1949 prect->bottom -= rt.bottom;
1953 static BOOL toggle_fullscreen(HWND hwnd)
1955 RECT rt;
1957 if ((g_fullscreen.mode=!g_fullscreen.mode)) {
1958 GetWindowRect(hwnd, &g_fullscreen.orgPos);
1959 g_fullscreen.wasZoomed = IsZoomed(hwnd);
1961 Frame_CalcFrameClient(hwnd, &rt);
1962 MapWindowPoints( hwnd, 0, (POINT *)&rt, 2 );
1964 rt.left = g_fullscreen.orgPos.left-rt.left;
1965 rt.top = g_fullscreen.orgPos.top-rt.top;
1966 rt.right = GetSystemMetrics(SM_CXSCREEN)+g_fullscreen.orgPos.right-rt.right;
1967 rt.bottom = GetSystemMetrics(SM_CYSCREEN)+g_fullscreen.orgPos.bottom-rt.bottom;
1969 MoveWindow(hwnd, rt.left, rt.top, rt.right-rt.left, rt.bottom-rt.top, TRUE);
1970 } else {
1971 MoveWindow(hwnd, g_fullscreen.orgPos.left, g_fullscreen.orgPos.top,
1972 g_fullscreen.orgPos.right-g_fullscreen.orgPos.left,
1973 g_fullscreen.orgPos.bottom-g_fullscreen.orgPos.top, TRUE);
1975 if (g_fullscreen.wasZoomed)
1976 ShowWindow(hwnd, WS_MAXIMIZE);
1979 return g_fullscreen.mode;
1982 static void fullscreen_move(HWND hwnd)
1984 RECT rt, pos;
1985 GetWindowRect(hwnd, &pos);
1987 Frame_CalcFrameClient(hwnd, &rt);
1988 MapWindowPoints( hwnd, 0, (POINT *)&rt, 2 );
1990 rt.left = pos.left-rt.left;
1991 rt.top = pos.top-rt.top;
1992 rt.right = GetSystemMetrics(SM_CXSCREEN)+pos.right-rt.right;
1993 rt.bottom = GetSystemMetrics(SM_CYSCREEN)+pos.bottom-rt.bottom;
1995 MoveWindow(hwnd, rt.left, rt.top, rt.right-rt.left, rt.bottom-rt.top, TRUE);
1998 static void toggle_child(HWND hwnd, UINT cmd, HWND hchild)
2000 BOOL vis = IsWindowVisible(hchild);
2002 CheckMenuItem(Globals.hMenuOptions, cmd, vis?MF_BYCOMMAND:MF_BYCOMMAND|MF_CHECKED);
2004 ShowWindow(hchild, vis?SW_HIDE:SW_SHOW);
2006 if (g_fullscreen.mode)
2007 fullscreen_move(hwnd);
2009 resize_frame_client(hwnd);
2012 static BOOL activate_drive_window(LPCWSTR path)
2014 WCHAR drv1[_MAX_DRIVE], drv2[_MAX_DRIVE];
2015 HWND child_wnd;
2017 _wsplitpath(path, drv1, 0, 0, 0);
2019 /* search for an already open window for the same drive */
2020 for(child_wnd=GetNextWindow(Globals.hmdiclient,GW_CHILD); child_wnd; child_wnd=GetNextWindow(child_wnd, GW_HWNDNEXT)) {
2021 ChildWnd* child = (ChildWnd*)GetWindowLongPtrW(child_wnd, GWLP_USERDATA);
2023 if (child) {
2024 _wsplitpath(child->root.path, drv2, 0, 0, 0);
2026 if (!lstrcmpiW(drv2, drv1)) {
2027 SendMessageW(Globals.hmdiclient, WM_MDIACTIVATE, (WPARAM)child_wnd, 0);
2029 if (IsIconic(child_wnd))
2030 ShowWindow(child_wnd, SW_SHOWNORMAL);
2032 return TRUE;
2037 return FALSE;
2040 static BOOL activate_fs_window(LPCWSTR filesys)
2042 HWND child_wnd;
2044 /* search for an already open window of the given file system name */
2045 for(child_wnd=GetNextWindow(Globals.hmdiclient,GW_CHILD); child_wnd; child_wnd=GetNextWindow(child_wnd, GW_HWNDNEXT)) {
2046 ChildWnd* child = (ChildWnd*) GetWindowLongPtrW(child_wnd, GWLP_USERDATA);
2048 if (child) {
2049 if (!lstrcmpiW(child->root.fs, filesys)) {
2050 SendMessageW(Globals.hmdiclient, WM_MDIACTIVATE, (WPARAM)child_wnd, 0);
2052 if (IsIconic(child_wnd))
2053 ShowWindow(child_wnd, SW_SHOWNORMAL);
2055 return TRUE;
2060 return FALSE;
2063 static LRESULT CALLBACK FrameWndProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam)
2065 WCHAR b1[BUFFER_LEN], b2[BUFFER_LEN];
2067 switch(nmsg) {
2068 case WM_CLOSE:
2069 if (Globals.saveSettings)
2070 save_registry_settings();
2072 DestroyWindow(hwnd);
2074 /* clear handle variables */
2075 Globals.hMenuFrame = 0;
2076 Globals.hMenuView = 0;
2077 Globals.hMenuOptions = 0;
2078 Globals.hMainWnd = 0;
2079 Globals.hmdiclient = 0;
2080 Globals.hdrivebar = 0;
2081 break;
2083 case WM_DESTROY:
2084 PostQuitMessage(0);
2085 break;
2087 case WM_INITMENUPOPUP: {
2088 HWND hwndClient = (HWND)SendMessageW(Globals.hmdiclient, WM_MDIGETACTIVE, 0, 0);
2090 if (!SendMessageW(hwndClient, WM_INITMENUPOPUP, wparam, lparam))
2091 return 0;
2092 break;}
2094 case WM_COMMAND: {
2095 UINT cmd = LOWORD(wparam);
2096 HWND hwndClient = (HWND)SendMessageW(Globals.hmdiclient, WM_MDIGETACTIVE, 0, 0);
2098 if (SendMessageW(hwndClient, WM_DISPATCH_COMMAND, wparam, lparam))
2099 break;
2101 if (cmd>=ID_DRIVE_FIRST && cmd<=ID_DRIVE_FIRST+0xFF) {
2102 WCHAR drv[_MAX_DRIVE], path[MAX_PATH];
2103 ChildWnd* child;
2104 LPCWSTR root = Globals.drives;
2105 int i;
2107 for(i=cmd-ID_DRIVE_FIRST; i--; root++)
2108 while(*root)
2109 root++;
2111 if (activate_drive_window(root))
2112 return 0;
2114 _wsplitpath(root, drv, 0, 0, 0);
2116 if (!SetCurrentDirectoryW(drv)) {
2117 display_error(hwnd, GetLastError());
2118 return 0;
2121 GetCurrentDirectoryW(MAX_PATH, path); /*TODO: store last directory per drive */
2122 child = alloc_child_window(path, NULL, hwnd);
2124 if (!create_child_window(child))
2125 HeapFree(GetProcessHeap(), 0, child);
2126 } else switch(cmd) {
2127 case ID_FILE_EXIT:
2128 SendMessageW(hwnd, WM_CLOSE, 0, 0);
2129 break;
2131 case ID_WINDOW_NEW: {
2132 WCHAR path[MAX_PATH];
2133 ChildWnd* child;
2135 GetCurrentDirectoryW(MAX_PATH, path);
2136 child = alloc_child_window(path, NULL, hwnd);
2138 if (!create_child_window(child))
2139 HeapFree(GetProcessHeap(), 0, child);
2140 break;}
2142 case ID_REFRESH:
2143 refresh_drives();
2144 break;
2146 case ID_WINDOW_CASCADE:
2147 SendMessageW(Globals.hmdiclient, WM_MDICASCADE, 0, 0);
2148 break;
2150 case ID_WINDOW_TILE_HORZ:
2151 SendMessageW(Globals.hmdiclient, WM_MDITILE, MDITILE_HORIZONTAL, 0);
2152 break;
2154 case ID_WINDOW_TILE_VERT:
2155 SendMessageW(Globals.hmdiclient, WM_MDITILE, MDITILE_VERTICAL, 0);
2156 break;
2158 case ID_WINDOW_ARRANGE:
2159 SendMessageW(Globals.hmdiclient, WM_MDIICONARRANGE, 0, 0);
2160 break;
2162 case ID_SELECT_FONT:
2163 choose_font(hwnd);
2164 break;
2166 case ID_VIEW_TOOL_BAR:
2167 toggle_child(hwnd, cmd, Globals.htoolbar);
2168 break;
2170 case ID_VIEW_DRIVE_BAR:
2171 toggle_child(hwnd, cmd, Globals.hdrivebar);
2172 break;
2174 case ID_VIEW_STATUSBAR:
2175 toggle_child(hwnd, cmd, Globals.hstatusbar);
2176 break;
2178 case ID_VIEW_SAVESETTINGS:
2179 Globals.saveSettings = !Globals.saveSettings;
2180 CheckMenuItem(Globals.hMenuOptions, ID_VIEW_SAVESETTINGS,
2181 Globals.saveSettings ? MF_CHECKED : MF_UNCHECKED );
2182 break;
2184 case ID_RUN:
2185 WineFile_OnRun( hwnd );
2186 break;
2188 case ID_CONNECT_NETWORK_DRIVE: {
2189 DWORD ret = WNetConnectionDialog(hwnd, RESOURCETYPE_DISK);
2190 if (ret == NO_ERROR)
2191 refresh_drives();
2192 else if (ret != (DWORD)-1) {
2193 if (ret == ERROR_EXTENDED_ERROR)
2194 display_network_error(hwnd);
2195 else
2196 display_error(hwnd, ret);
2198 break;}
2200 case ID_DISCONNECT_NETWORK_DRIVE: {
2201 DWORD ret = WNetDisconnectDialog(hwnd, RESOURCETYPE_DISK);
2202 if (ret == NO_ERROR)
2203 refresh_drives();
2204 else if (ret != (DWORD)-1) {
2205 if (ret == ERROR_EXTENDED_ERROR)
2206 display_network_error(hwnd);
2207 else
2208 display_error(hwnd, ret);
2210 break;}
2212 case ID_HELP:
2213 WinHelpW(hwnd, RS(b1,IDS_WINEFILE), HELP_INDEX, 0);
2214 break;
2216 case ID_VIEW_FULLSCREEN:
2217 CheckMenuItem(Globals.hMenuOptions, cmd, toggle_fullscreen(hwnd)?MF_CHECKED:0);
2218 break;
2220 #ifdef __WINE__
2221 case ID_DRIVE_UNIX_FS: {
2222 WCHAR path[MAX_PATH];
2223 char cpath[MAX_PATH];
2224 ChildWnd* child;
2226 if (activate_fs_window(RS(b1,IDS_UNIXFS)))
2227 break;
2229 getcwd(cpath, MAX_PATH);
2230 MultiByteToWideChar(CP_UNIXCP, 0, cpath, -1, path, MAX_PATH);
2231 child = alloc_child_window(path, NULL, hwnd);
2233 if (!create_child_window(child))
2234 HeapFree(GetProcessHeap(), 0, child);
2235 break;}
2236 #endif
2237 case ID_DRIVE_SHELL_NS: {
2238 WCHAR path[MAX_PATH];
2239 ChildWnd* child;
2241 if (activate_fs_window(RS(b1,IDS_SHELL)))
2242 break;
2244 GetCurrentDirectoryW(MAX_PATH, path);
2245 child = alloc_child_window(path, get_path_pidl(path,hwnd), hwnd);
2247 if (!create_child_window(child))
2248 HeapFree(GetProcessHeap(), 0, child);
2249 break;}
2251 /*TODO: There are even more menu items! */
2253 case ID_ABOUT:
2254 ShellAboutW(hwnd, RS(b1,IDS_WINEFILE), NULL,
2255 LoadImageW( Globals.hInstance, MAKEINTRESOURCEW(IDI_WINEFILE),
2256 IMAGE_ICON, 48, 48, LR_SHARED ));
2257 break;
2259 default:
2260 /*TODO: if (wParam >= PM_FIRST_LANGUAGE && wParam <= PM_LAST_LANGUAGE)
2261 STRING_SelectLanguageByNumber(wParam - PM_FIRST_LANGUAGE);
2262 else */if ((cmd<IDW_FIRST_CHILD || cmd>=IDW_FIRST_CHILD+0x100) &&
2263 (cmd<SC_SIZE || cmd>SC_RESTORE))
2264 MessageBoxW(hwnd, RS(b2,IDS_NO_IMPL), RS(b1,IDS_WINEFILE), MB_OK);
2266 return DefFrameProcW(hwnd, Globals.hmdiclient, nmsg, wparam, lparam);
2268 break;}
2270 case WM_SIZE:
2271 resize_frame(hwnd, LOWORD(lparam), HIWORD(lparam));
2272 break; /* do not pass message to DefFrameProcW */
2274 case WM_DEVICECHANGE:
2275 SendMessageW(hwnd, WM_COMMAND, MAKELONG(ID_REFRESH,0), 0);
2276 break;
2278 case WM_GETMINMAXINFO: {
2279 LPMINMAXINFO lpmmi = (LPMINMAXINFO)lparam;
2281 lpmmi->ptMaxTrackSize.x <<= 1;/*2*GetSystemMetrics(SM_CXSCREEN) / SM_CXVIRTUALSCREEN */
2282 lpmmi->ptMaxTrackSize.y <<= 1;/*2*GetSystemMetrics(SM_CYSCREEN) / SM_CYVIRTUALSCREEN */
2283 break;}
2285 case FRM_CALC_CLIENT:
2286 frame_get_clientspace(hwnd, (PRECT)lparam);
2287 return TRUE;
2289 default:
2290 return DefFrameProcW(hwnd, Globals.hmdiclient, nmsg, wparam, lparam);
2293 return 0;
2297 static WCHAR g_pos_names[COLUMNS][40] = {
2298 {'\0'} /* symbol */
2301 static const int g_pos_align[] = {
2303 HDF_LEFT, /* Name */
2304 HDF_RIGHT, /* Size */
2305 HDF_LEFT, /* CDate */
2306 HDF_LEFT, /* ADate */
2307 HDF_LEFT, /* MDate */
2308 HDF_LEFT, /* Index */
2309 HDF_CENTER, /* Links */
2310 HDF_CENTER, /* Attributes */
2311 HDF_LEFT /* Security */
2314 static void resize_tree(ChildWnd* child, int cx, int cy)
2316 HDWP hdwp = BeginDeferWindowPos(4);
2317 RECT rt;
2318 WINDOWPOS wp;
2319 HD_LAYOUT hdl;
2321 rt.left = 0;
2322 rt.top = 0;
2323 rt.right = cx;
2324 rt.bottom = cy;
2326 cx = child->split_pos + SPLIT_WIDTH/2;
2327 hdl.prc = &rt;
2328 hdl.pwpos = &wp;
2330 SendMessageW(child->left.hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hdl);
2332 DeferWindowPos(hdwp, child->left.hwndHeader, wp.hwndInsertAfter,
2333 wp.x-1, wp.y, child->split_pos-SPLIT_WIDTH/2+1, wp.cy, wp.flags);
2334 DeferWindowPos(hdwp, child->right.hwndHeader, wp.hwndInsertAfter,
2335 rt.left+cx+1, wp.y, wp.cx-cx+2, wp.cy, wp.flags);
2336 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);
2337 DeferWindowPos(hdwp, child->right.hwnd, 0, rt.left+cx+1, rt.top, rt.right-cx, rt.bottom-rt.top, SWP_NOZORDER|SWP_NOACTIVATE);
2339 EndDeferWindowPos(hdwp);
2342 static HWND create_header(HWND parent, Pane* pane, UINT id)
2344 HDITEMW hdi;
2345 int idx;
2347 HWND hwnd = CreateWindowW(WC_HEADERW, 0, WS_CHILD|WS_VISIBLE|HDS_HORZ|HDS_FULLDRAG/*TODO: |HDS_BUTTONS + sort orders*/,
2348 0, 0, 0, 0, parent, (HMENU)ULongToHandle(id), Globals.hInstance, 0);
2349 if (!hwnd)
2350 return 0;
2352 SendMessageW(hwnd, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), FALSE);
2354 hdi.mask = HDI_TEXT|HDI_WIDTH|HDI_FORMAT;
2356 for(idx=0; idx<COLUMNS; idx++) {
2357 hdi.pszText = g_pos_names[idx];
2358 hdi.fmt = HDF_STRING | g_pos_align[idx];
2359 hdi.cxy = pane->widths[idx];
2360 pane->widths_shown[idx] = hdi.cxy;
2361 SendMessageW(hwnd, HDM_INSERTITEMW, idx, (LPARAM)&hdi);
2364 return hwnd;
2367 static void init_output(HWND hwnd)
2369 static const WCHAR s1000[] = {'1','0','0','0','\0'};
2370 WCHAR b[16];
2371 HFONT old_font;
2372 HDC hdc = GetDC(hwnd);
2374 if (GetNumberFormatW(LOCALE_USER_DEFAULT, 0, s1000, 0, b, 16) > 4)
2375 Globals.num_sep = b[1];
2376 else
2377 Globals.num_sep = '.';
2379 old_font = SelectObject(hdc, Globals.hfont);
2380 GetTextExtentPoint32W(hdc, sSpace, 1, &Globals.spaceSize);
2381 SelectObject(hdc, old_font);
2382 ReleaseDC(hwnd, hdc);
2385 static void draw_item(Pane* pane, LPDRAWITEMSTRUCT dis, Entry* entry, int calcWidthCol);
2388 /* calculate preferred width for all visible columns */
2390 static BOOL calc_widths(Pane* pane, BOOL anyway)
2392 int col, x, cx, spc=3*Globals.spaceSize.cx;
2393 int entries = SendMessageW(pane->hwnd, LB_GETCOUNT, 0, 0);
2394 int orgWidths[COLUMNS];
2395 int orgPositions[COLUMNS+1];
2396 HFONT hfontOld;
2397 HDC hdc;
2398 int cnt;
2400 if (!anyway) {
2401 memcpy(orgWidths, pane->widths, sizeof(orgWidths));
2402 memcpy(orgPositions, pane->positions, sizeof(orgPositions));
2405 for(col=0; col<COLUMNS; col++)
2406 pane->widths[col] = 0;
2408 hdc = GetDC(pane->hwnd);
2409 hfontOld = SelectObject(hdc, Globals.hfont);
2411 for(cnt=0; cnt<entries; cnt++) {
2412 Entry* entry = (Entry*)SendMessageW(pane->hwnd, LB_GETITEMDATA, cnt, 0);
2414 DRAWITEMSTRUCT dis;
2416 dis.CtlType = 0;
2417 dis.CtlID = 0;
2418 dis.itemID = 0;
2419 dis.itemAction = 0;
2420 dis.itemState = 0;
2421 dis.hwndItem = pane->hwnd;
2422 dis.hDC = hdc;
2423 SetRectEmpty(&dis.rcItem);
2424 /*dis.itemData = 0; */
2426 draw_item(pane, &dis, entry, COLUMNS);
2429 SelectObject(hdc, hfontOld);
2430 ReleaseDC(pane->hwnd, hdc);
2432 x = 0;
2433 for(col=0; col<COLUMNS; col++) {
2434 pane->positions[col] = x;
2435 cx = pane->widths[col];
2437 if (cx) {
2438 cx += spc;
2440 if (cx < IMAGE_WIDTH)
2441 cx = IMAGE_WIDTH;
2443 pane->widths[col] = cx;
2446 x += cx;
2449 pane->positions[COLUMNS] = x;
2451 SendMessageW(pane->hwnd, LB_SETHORIZONTALEXTENT, x, 0);
2453 /* no change? */
2454 if (!anyway && !memcmp(orgWidths, pane->widths, sizeof(orgWidths)))
2455 return FALSE;
2457 /* don't move, if only collapsing an entry */
2458 if (!anyway && pane->widths[0]<orgWidths[0] &&
2459 !memcmp(orgWidths+1, pane->widths+1, sizeof(orgWidths)-sizeof(int))) {
2460 pane->widths[0] = orgWidths[0];
2461 memcpy(pane->positions, orgPositions, sizeof(orgPositions));
2463 return FALSE;
2466 InvalidateRect(pane->hwnd, 0, TRUE);
2468 return TRUE;
2471 /* calculate one preferred column width */
2472 static void calc_single_width(Pane* pane, int col)
2474 HFONT hfontOld;
2475 int x, cx;
2476 int entries = SendMessageW(pane->hwnd, LB_GETCOUNT, 0, 0);
2477 int cnt;
2478 HDC hdc;
2480 pane->widths[col] = 0;
2482 hdc = GetDC(pane->hwnd);
2483 hfontOld = SelectObject(hdc, Globals.hfont);
2485 for(cnt=0; cnt<entries; cnt++) {
2486 Entry* entry = (Entry*)SendMessageW(pane->hwnd, LB_GETITEMDATA, cnt, 0);
2487 DRAWITEMSTRUCT dis;
2489 dis.CtlType = 0;
2490 dis.CtlID = 0;
2491 dis.itemID = 0;
2492 dis.itemAction = 0;
2493 dis.itemState = 0;
2494 dis.hwndItem = pane->hwnd;
2495 dis.hDC = hdc;
2496 SetRectEmpty(&dis.rcItem);
2497 /*dis.itemData = 0; */
2499 draw_item(pane, &dis, entry, col);
2502 SelectObject(hdc, hfontOld);
2503 ReleaseDC(pane->hwnd, hdc);
2505 cx = pane->widths[col];
2507 if (cx) {
2508 cx += 3*Globals.spaceSize.cx;
2510 if (cx < IMAGE_WIDTH)
2511 cx = IMAGE_WIDTH;
2514 pane->widths[col] = cx;
2516 x = pane->positions[col] + cx;
2518 for(; col<COLUMNS-1; ) {
2519 pane->positions[++col] = x;
2520 x += pane->widths[col];
2523 SendMessageW(pane->hwnd, LB_SETHORIZONTALEXTENT, x, 0);
2526 static BOOL pattern_match(LPCWSTR str, LPCWSTR pattern)
2528 for( ; *str&&*pattern; str++,pattern++) {
2529 if (*pattern == '*') {
2530 do pattern++;
2531 while(*pattern == '*');
2533 if (!*pattern)
2534 return TRUE;
2536 for(; *str; str++)
2537 if (*str==*pattern && pattern_match(str, pattern))
2538 return TRUE;
2540 return FALSE;
2542 else if (*str!=*pattern && *pattern!='?')
2543 return FALSE;
2546 if (*str || *pattern)
2547 if (*pattern!='*' || pattern[1]!='\0')
2548 return FALSE;
2550 return TRUE;
2553 static BOOL pattern_imatch(LPCWSTR str, LPCWSTR pattern)
2555 WCHAR b1[BUFFER_LEN], b2[BUFFER_LEN];
2557 lstrcpyW(b1, str);
2558 lstrcpyW(b2, pattern);
2559 CharUpperW(b1);
2560 CharUpperW(b2);
2562 return pattern_match(b1, b2);
2566 enum FILE_TYPE {
2567 FT_OTHER = 0,
2568 FT_EXECUTABLE = 1,
2569 FT_DOCUMENT = 2
2572 static enum FILE_TYPE get_file_type(LPCWSTR filename);
2575 /* insert listbox entries after index idx */
2577 static int insert_entries(Pane* pane, Entry* dir, LPCWSTR pattern, int filter_flags, int idx)
2579 Entry* entry = dir;
2581 if (!entry)
2582 return idx;
2584 ShowWindow(pane->hwnd, SW_HIDE);
2586 for(; entry; entry=entry->next) {
2587 if (pane->treePane && !(entry->data.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY))
2588 continue;
2590 if (entry->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
2591 /* don't display entries "." and ".." in the left pane */
2592 if (pane->treePane && entry->data.cFileName[0] == '.')
2593 if (entry->data.cFileName[1] == '\0' ||
2594 (entry->data.cFileName[1] == '.' &&
2595 entry->data.cFileName[2] == '\0'))
2596 continue;
2598 /* filter directories in right pane */
2599 if (!pane->treePane && !(filter_flags&TF_DIRECTORIES))
2600 continue;
2603 /* filter using the file name pattern */
2604 if (pattern)
2605 if (!pattern_imatch(entry->data.cFileName, pattern))
2606 continue;
2608 /* filter system and hidden files */
2609 if (!(filter_flags&TF_HIDDEN) && (entry->data.dwFileAttributes&(FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM)))
2610 continue;
2612 /* filter looking at the file type */
2613 if ((filter_flags&(TF_PROGRAMS|TF_DOCUMENTS|TF_OTHERS)) != (TF_PROGRAMS|TF_DOCUMENTS|TF_OTHERS))
2614 switch(get_file_type(entry->data.cFileName)) {
2615 case FT_EXECUTABLE:
2616 if (!(filter_flags & TF_PROGRAMS))
2617 continue;
2618 break;
2620 case FT_DOCUMENT:
2621 if (!(filter_flags & TF_DOCUMENTS))
2622 continue;
2623 break;
2625 default: /* TF_OTHERS */
2626 if (!(filter_flags & TF_OTHERS))
2627 continue;
2630 if (idx != -1)
2631 idx++;
2633 SendMessageW(pane->hwnd, LB_INSERTSTRING, idx, (LPARAM)entry);
2635 if (pane->treePane && entry->expanded)
2636 idx = insert_entries(pane, entry->down, pattern, filter_flags, idx);
2639 ShowWindow(pane->hwnd, SW_SHOW);
2641 return idx;
2644 static void set_space_status(void)
2646 ULARGE_INTEGER ulFreeBytesToCaller, ulTotalBytes, ulFreeBytes;
2647 WCHAR fmt[64], b1[64], b2[64], buffer[BUFFER_LEN];
2649 if (GetDiskFreeSpaceExW(NULL, &ulFreeBytesToCaller, &ulTotalBytes, &ulFreeBytes)) {
2650 DWORD_PTR args[2];
2652 args[0] = (DWORD_PTR)StrFormatByteSizeW(ulFreeBytesToCaller.QuadPart, b1, sizeof(b1)/sizeof(*b1));
2653 args[1] = (DWORD_PTR)StrFormatByteSizeW(ulTotalBytes.QuadPart, b2, sizeof(b2)/sizeof(*b2));
2655 FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
2656 RS(fmt,IDS_FREE_SPACE_FMT), 0, 0, buffer,
2657 sizeof(buffer)/sizeof(*buffer), (__ms_va_list*)args);
2658 } else
2659 lstrcpyW(buffer, sQMarks);
2661 SendMessageW(Globals.hstatusbar, SB_SETTEXTW, 0, (LPARAM)buffer);
2665 static WNDPROC g_orgTreeWndProc;
2667 static void create_tree_window(HWND parent, Pane* pane, UINT id, UINT id_header, LPCWSTR pattern, int filter_flags)
2669 static const WCHAR sListBox[] = {'L','i','s','t','B','o','x','\0'};
2671 static BOOL s_init = FALSE;
2672 Entry* entry = pane->root;
2674 pane->hwnd = CreateWindowW(sListBox, sEmpty, WS_CHILD|WS_VISIBLE|WS_HSCROLL|WS_VSCROLL|
2675 LBS_DISABLENOSCROLL|LBS_NOINTEGRALHEIGHT|LBS_OWNERDRAWFIXED|LBS_NOTIFY,
2676 0, 0, 0, 0, parent, (HMENU)ULongToHandle(id), Globals.hInstance, 0);
2678 SetWindowLongPtrW(pane->hwnd, GWLP_USERDATA, (LPARAM)pane);
2679 g_orgTreeWndProc = (WNDPROC)SetWindowLongPtrW(pane->hwnd, GWLP_WNDPROC, (LPARAM)TreeWndProc);
2681 SendMessageW(pane->hwnd, WM_SETFONT, (WPARAM)Globals.hfont, FALSE);
2683 /* insert entries into listbox */
2684 if (entry)
2685 insert_entries(pane, entry, pattern, filter_flags, -1);
2687 /* calculate column widths */
2688 if (!s_init) {
2689 s_init = TRUE;
2690 init_output(pane->hwnd);
2693 calc_widths(pane, TRUE);
2695 pane->hwndHeader = create_header(parent, pane, id_header);
2699 static void InitChildWindow(ChildWnd* child)
2701 create_tree_window(child->hwnd, &child->left, IDW_TREE_LEFT, IDW_HEADER_LEFT, NULL, TF_ALL);
2702 create_tree_window(child->hwnd, &child->right, IDW_TREE_RIGHT, IDW_HEADER_RIGHT, child->filter_pattern, child->filter_flags);
2706 static void format_date(const FILETIME* ft, WCHAR* buffer, int visible_cols)
2708 SYSTEMTIME systime;
2709 FILETIME lft;
2710 int len = 0;
2712 *buffer = '\0';
2714 if (!ft->dwLowDateTime && !ft->dwHighDateTime)
2715 return;
2717 if (!FileTimeToLocalFileTime(ft, &lft))
2718 {err: lstrcpyW(buffer,sQMarks); return;}
2720 if (!FileTimeToSystemTime(&lft, &systime))
2721 goto err;
2723 if (visible_cols & COL_DATE) {
2724 len = GetDateFormatW(LOCALE_USER_DEFAULT, 0, &systime, 0, buffer, BUFFER_LEN);
2725 if (!len)
2726 goto err;
2729 if (visible_cols & COL_TIME) {
2730 if (len)
2731 buffer[len-1] = ' ';
2733 buffer[len++] = ' ';
2735 if (!GetTimeFormatW(LOCALE_USER_DEFAULT, 0, &systime, 0, buffer+len, BUFFER_LEN-len))
2736 buffer[len] = '\0';
2741 static void calc_width(Pane* pane, LPDRAWITEMSTRUCT dis, int col, LPCWSTR str)
2743 RECT rt = {0, 0, 0, 0};
2745 DrawTextW(dis->hDC, str, -1, &rt, DT_CALCRECT|DT_SINGLELINE|DT_NOPREFIX);
2747 if (rt.right > pane->widths[col])
2748 pane->widths[col] = rt.right;
2751 static void calc_tabbed_width(Pane* pane, LPDRAWITEMSTRUCT dis, int col, LPCWSTR str)
2753 RECT rt = {0, 0, 0, 0};
2755 DrawTextW(dis->hDC, str, -1, &rt, DT_CALCRECT|DT_SINGLELINE|DT_EXPANDTABS|DT_TABSTOP|(2<<8));
2756 /*FIXME rt (0,0) ??? */
2758 if (rt.right > pane->widths[col])
2759 pane->widths[col] = rt.right;
2763 static void output_text(Pane* pane, LPDRAWITEMSTRUCT dis, int col, LPCWSTR str, DWORD flags)
2765 int x = dis->rcItem.left;
2766 RECT rt;
2768 rt.left = x+pane->positions[col]+Globals.spaceSize.cx;
2769 rt.top = dis->rcItem.top;
2770 rt.right = x+pane->positions[col+1]-Globals.spaceSize.cx;
2771 rt.bottom = dis->rcItem.bottom;
2773 DrawTextW(dis->hDC, str, -1, &rt, DT_SINGLELINE|DT_NOPREFIX|flags);
2776 static void output_tabbed_text(Pane* pane, LPDRAWITEMSTRUCT dis, int col, LPCWSTR str)
2778 int x = dis->rcItem.left;
2779 RECT rt;
2781 rt.left = x+pane->positions[col]+Globals.spaceSize.cx;
2782 rt.top = dis->rcItem.top;
2783 rt.right = x+pane->positions[col+1]-Globals.spaceSize.cx;
2784 rt.bottom = dis->rcItem.bottom;
2786 DrawTextW(dis->hDC, str, -1, &rt, DT_SINGLELINE|DT_EXPANDTABS|DT_TABSTOP|(2<<8));
2789 static void output_number(Pane* pane, LPDRAWITEMSTRUCT dis, int col, LPCWSTR str)
2791 int x = dis->rcItem.left;
2792 RECT rt;
2793 LPCWSTR s = str;
2794 WCHAR b[128];
2795 LPWSTR d = b;
2796 int pos;
2798 rt.left = x+pane->positions[col]+Globals.spaceSize.cx;
2799 rt.top = dis->rcItem.top;
2800 rt.right = x+pane->positions[col+1]-Globals.spaceSize.cx;
2801 rt.bottom = dis->rcItem.bottom;
2803 if (*s)
2804 *d++ = *s++;
2806 /* insert number separator characters */
2807 pos = lstrlenW(s) % 3;
2809 while(*s)
2810 if (pos--)
2811 *d++ = *s++;
2812 else {
2813 *d++ = Globals.num_sep;
2814 pos = 3;
2817 DrawTextW(dis->hDC, b, d-b, &rt, DT_RIGHT|DT_SINGLELINE|DT_NOPREFIX|DT_END_ELLIPSIS);
2821 static BOOL is_exe_file(LPCWSTR ext)
2823 static const WCHAR executable_extensions[][4] = {
2824 {'C','O','M','\0'},
2825 {'E','X','E','\0'},
2826 {'B','A','T','\0'},
2827 {'C','M','D','\0'},
2828 {'C','M','M','\0'},
2829 {'B','T','M','\0'},
2830 {'A','W','K','\0'},
2831 {'\0'}
2834 WCHAR ext_buffer[_MAX_EXT];
2835 const WCHAR (*p)[4];
2836 LPCWSTR s;
2837 LPWSTR d;
2839 for(s=ext+1,d=ext_buffer; (*d=tolower(*s)); s++)
2840 d++;
2842 for(p=executable_extensions; (*p)[0]; p++)
2843 if (!lstrcmpiW(ext_buffer, *p))
2844 return TRUE;
2846 return FALSE;
2849 static BOOL is_registered_type(LPCWSTR ext)
2851 /* check if there exists a classname for this file extension in the registry */
2852 if (!RegQueryValueW(HKEY_CLASSES_ROOT, ext, NULL, NULL))
2853 return TRUE;
2855 return FALSE;
2858 static enum FILE_TYPE get_file_type(LPCWSTR filename)
2860 LPCWSTR ext = strrchrW(filename, '.');
2861 if (!ext)
2862 ext = sEmpty;
2864 if (is_exe_file(ext))
2865 return FT_EXECUTABLE;
2866 else if (is_registered_type(ext))
2867 return FT_DOCUMENT;
2868 else
2869 return FT_OTHER;
2873 static void draw_item(Pane* pane, LPDRAWITEMSTRUCT dis, Entry* entry, int calcWidthCol)
2875 WCHAR buffer[BUFFER_LEN];
2876 DWORD attrs;
2877 int visible_cols = pane->visible_cols;
2878 COLORREF bkcolor, textcolor;
2879 RECT focusRect = dis->rcItem;
2880 HBRUSH hbrush;
2881 enum IMAGE img;
2882 int img_pos, cx;
2883 int col = 0;
2885 if (entry) {
2886 attrs = entry->data.dwFileAttributes;
2888 if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
2889 if (entry->data.cFileName[0] == '.' && entry->data.cFileName[1] == '.'
2890 && entry->data.cFileName[2] == '\0')
2891 img = IMG_FOLDER_UP;
2892 else if (entry->data.cFileName[0] == '.' && entry->data.cFileName[1] == '\0')
2893 img = IMG_FOLDER_CUR;
2894 else if (pane->treePane && (dis->itemState&ODS_FOCUS))
2895 img = IMG_OPEN_FOLDER;
2896 else
2897 img = IMG_FOLDER;
2898 } else {
2899 switch(get_file_type(entry->data.cFileName)) {
2900 case FT_EXECUTABLE: img = IMG_EXECUTABLE; break;
2901 case FT_DOCUMENT: img = IMG_DOCUMENT; break;
2902 default: img = IMG_FILE;
2905 } else {
2906 attrs = 0;
2907 img = IMG_NONE;
2910 if (pane->treePane) {
2911 if (entry) {
2912 img_pos = dis->rcItem.left + entry->level*(IMAGE_WIDTH+TREE_LINE_DX);
2914 if (calcWidthCol == -1) {
2915 int x;
2916 int y = dis->rcItem.top + IMAGE_HEIGHT/2;
2917 Entry* up;
2918 RECT rt_clip;
2919 HRGN hrgn_org = CreateRectRgn(0, 0, 0, 0);
2920 HRGN hrgn;
2922 rt_clip.left = dis->rcItem.left;
2923 rt_clip.top = dis->rcItem.top;
2924 rt_clip.right = dis->rcItem.left+pane->widths[col];
2925 rt_clip.bottom = dis->rcItem.bottom;
2927 hrgn = CreateRectRgnIndirect(&rt_clip);
2929 if (!GetClipRgn(dis->hDC, hrgn_org)) {
2930 DeleteObject(hrgn_org);
2931 hrgn_org = 0;
2934 ExtSelectClipRgn(dis->hDC, hrgn, RGN_AND);
2935 DeleteObject(hrgn);
2937 if ((up=entry->up) != NULL) {
2938 MoveToEx(dis->hDC, img_pos-IMAGE_WIDTH/2, y, 0);
2939 LineTo(dis->hDC, img_pos-2, y);
2941 x = img_pos - IMAGE_WIDTH/2;
2943 do {
2944 x -= IMAGE_WIDTH+TREE_LINE_DX;
2946 if (up->next
2947 && (up->next->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
2949 MoveToEx(dis->hDC, x, dis->rcItem.top, 0);
2950 LineTo(dis->hDC, x, dis->rcItem.bottom);
2952 } while((up=up->up) != NULL);
2955 x = img_pos - IMAGE_WIDTH/2;
2957 MoveToEx(dis->hDC, x, dis->rcItem.top, 0);
2958 LineTo(dis->hDC, x, y);
2960 if (entry->next
2961 && (entry->next->data.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY))
2962 LineTo(dis->hDC, x, dis->rcItem.bottom);
2964 SelectClipRgn(dis->hDC, hrgn_org);
2965 if (hrgn_org) DeleteObject(hrgn_org);
2966 } else if (calcWidthCol==col || calcWidthCol==COLUMNS) {
2967 int right = img_pos + IMAGE_WIDTH - TREE_LINE_DX;
2969 if (right > pane->widths[col])
2970 pane->widths[col] = right;
2972 } else {
2973 img_pos = dis->rcItem.left;
2975 } else {
2976 img_pos = dis->rcItem.left;
2978 if (calcWidthCol==col || calcWidthCol==COLUMNS)
2979 pane->widths[col] = IMAGE_WIDTH;
2982 if (calcWidthCol == -1) {
2983 focusRect.left = img_pos -2;
2985 if (attrs & FILE_ATTRIBUTE_COMPRESSED)
2986 textcolor = COLOR_COMPRESSED;
2987 else
2988 textcolor = RGB(0,0,0);
2990 if (dis->itemState & ODS_FOCUS) {
2991 textcolor = RGB(255,255,255);
2992 bkcolor = COLOR_SELECTION;
2993 } else {
2994 bkcolor = RGB(255,255,255);
2997 hbrush = CreateSolidBrush(bkcolor);
2998 FillRect(dis->hDC, &focusRect, hbrush);
2999 DeleteObject(hbrush);
3001 SetBkMode(dis->hDC, TRANSPARENT);
3002 SetTextColor(dis->hDC, textcolor);
3004 cx = pane->widths[col];
3006 if (cx && img!=IMG_NONE) {
3007 if (cx > IMAGE_WIDTH)
3008 cx = IMAGE_WIDTH;
3010 if (entry->hicon && entry->hicon!=(HICON)-1)
3011 DrawIconEx(dis->hDC, img_pos, dis->rcItem.top, entry->hicon, cx, GetSystemMetrics(SM_CYSMICON), 0, 0, DI_NORMAL);
3012 else
3013 ImageList_DrawEx(Globals.himl, img, dis->hDC,
3014 img_pos, dis->rcItem.top, cx,
3015 IMAGE_HEIGHT, bkcolor, CLR_DEFAULT, ILD_NORMAL);
3019 if (!entry)
3020 return;
3022 col++;
3024 /* output file name */
3025 if (calcWidthCol == -1)
3026 output_text(pane, dis, col, entry->data.cFileName, 0);
3027 else if (calcWidthCol==col || calcWidthCol==COLUMNS)
3028 calc_width(pane, dis, col, entry->data.cFileName);
3030 col++;
3032 /* display file size */
3033 if (visible_cols & COL_SIZE) {
3034 format_longlong( buffer, ((ULONGLONG)entry->data.nFileSizeHigh << 32) | entry->data.nFileSizeLow );
3036 if (calcWidthCol == -1)
3037 output_number(pane, dis, col, buffer);
3038 else if (calcWidthCol==col || calcWidthCol==COLUMNS)
3039 calc_width(pane, dis, col, buffer);/*TODO: not ever time enough */
3041 col++;
3044 /* display file date */
3045 if (visible_cols & (COL_DATE|COL_TIME)) {
3046 format_date(&entry->data.ftCreationTime, buffer, visible_cols);
3047 if (calcWidthCol == -1)
3048 output_text(pane, dis, col, buffer, 0);
3049 else if (calcWidthCol==col || calcWidthCol==COLUMNS)
3050 calc_width(pane, dis, col, buffer);
3051 col++;
3053 format_date(&entry->data.ftLastAccessTime, buffer, visible_cols);
3054 if (calcWidthCol == -1)
3055 output_text(pane, dis, col, buffer, 0);
3056 else if (calcWidthCol==col || calcWidthCol==COLUMNS)
3057 calc_width(pane, dis, col, buffer);
3058 col++;
3060 format_date(&entry->data.ftLastWriteTime, buffer, visible_cols);
3061 if (calcWidthCol == -1)
3062 output_text(pane, dis, col, buffer, 0);
3063 else if (calcWidthCol==col || calcWidthCol==COLUMNS)
3064 calc_width(pane, dis, col, buffer);
3065 col++;
3068 if (entry->bhfi_valid) {
3069 if (visible_cols & COL_INDEX) {
3070 static const WCHAR fmtlow[] = {'%','X',0};
3071 static const WCHAR fmthigh[] = {'%','X','%','0','8','X',0};
3073 if (entry->bhfi.nFileIndexHigh)
3074 wsprintfW(buffer, fmthigh,
3075 entry->bhfi.nFileIndexHigh, entry->bhfi.nFileIndexLow );
3076 else
3077 wsprintfW(buffer, fmtlow, entry->bhfi.nFileIndexLow );
3079 if (calcWidthCol == -1)
3080 output_text(pane, dis, col, buffer, DT_RIGHT);
3081 else if (calcWidthCol==col || calcWidthCol==COLUMNS)
3082 calc_width(pane, dis, col, buffer);
3084 col++;
3087 if (visible_cols & COL_LINKS) {
3088 wsprintfW(buffer, sNumFmt, entry->bhfi.nNumberOfLinks);
3090 if (calcWidthCol == -1)
3091 output_text(pane, dis, col, buffer, DT_CENTER);
3092 else if (calcWidthCol==col || calcWidthCol==COLUMNS)
3093 calc_width(pane, dis, col, buffer);
3095 col++;
3097 } else
3098 col += 2;
3100 /* show file attributes */
3101 if (visible_cols & COL_ATTRIBUTES) {
3102 static const WCHAR s11Tabs[] = {' ','\t',' ','\t',' ','\t',' ','\t',' ','\t',' ','\t',' ','\t',' ','\t',' ','\t',' ','\t',' ','\t',' ','\0'};
3103 lstrcpyW(buffer, s11Tabs);
3105 if (attrs & FILE_ATTRIBUTE_NORMAL) buffer[ 0] = 'N';
3106 else {
3107 if (attrs & FILE_ATTRIBUTE_READONLY) buffer[ 2] = 'R';
3108 if (attrs & FILE_ATTRIBUTE_HIDDEN) buffer[ 4] = 'H';
3109 if (attrs & FILE_ATTRIBUTE_SYSTEM) buffer[ 6] = 'S';
3110 if (attrs & FILE_ATTRIBUTE_ARCHIVE) buffer[ 8] = 'A';
3111 if (attrs & FILE_ATTRIBUTE_COMPRESSED) buffer[10] = 'C';
3112 if (attrs & FILE_ATTRIBUTE_DIRECTORY) buffer[12] = 'D';
3113 if (attrs & FILE_ATTRIBUTE_ENCRYPTED) buffer[14] = 'E';
3114 if (attrs & FILE_ATTRIBUTE_TEMPORARY) buffer[16] = 'T';
3115 if (attrs & FILE_ATTRIBUTE_SPARSE_FILE) buffer[18] = 'P';
3116 if (attrs & FILE_ATTRIBUTE_REPARSE_POINT) buffer[20] = 'Q';
3117 if (attrs & FILE_ATTRIBUTE_OFFLINE) buffer[22] = 'O';
3118 if (attrs & FILE_ATTRIBUTE_NOT_CONTENT_INDEXED) buffer[24] = 'X';
3121 if (calcWidthCol == -1)
3122 output_tabbed_text(pane, dis, col, buffer);
3123 else if (calcWidthCol==col || calcWidthCol==COLUMNS)
3124 calc_tabbed_width(pane, dis, col, buffer);
3126 col++;
3130 static void set_header(Pane* pane)
3132 HDITEMW item;
3133 int scroll_pos = GetScrollPos(pane->hwnd, SB_HORZ);
3134 int i;
3136 item.mask = HDI_WIDTH;
3138 for (i = 0; i < COLUMNS; ++i) {
3139 if (pane->positions[i] >= scroll_pos) {
3140 item.cxy = pane->widths[i];
3141 } else if (pane->positions[i+1] <= scroll_pos) {
3142 item.cxy = 0;
3143 } else {
3144 item.cxy = pane->positions[i+1] - scroll_pos;
3146 pane->widths_shown[i] = item.cxy;
3147 SendMessageW(pane->hwndHeader, HDM_SETITEMW, i, (LPARAM)&item);
3151 static LRESULT pane_notify(Pane* pane, NMHDR* pnmh)
3153 switch(pnmh->code) {
3154 case HDN_ITEMCHANGEDW: {
3155 LPNMHEADERW phdn = (LPNMHEADERW)pnmh;
3156 int idx = phdn->iItem;
3157 int dx = phdn->pitem->cxy - pane->widths_shown[idx];
3158 int i;
3160 RECT clnt;
3161 GetClientRect(pane->hwnd, &clnt);
3163 pane->widths[idx] += dx;
3164 pane->widths_shown[idx] += dx;
3166 for(i=idx; ++i<=COLUMNS; )
3167 pane->positions[i] += dx;
3170 int scroll_pos = GetScrollPos(pane->hwnd, SB_HORZ);
3171 RECT rt_scr;
3172 RECT rt_clip;
3174 rt_scr.left = pane->positions[idx+1]-scroll_pos;
3175 rt_scr.top = 0;
3176 rt_scr.right = clnt.right;
3177 rt_scr.bottom = clnt.bottom;
3179 rt_clip.left = pane->positions[idx]-scroll_pos;
3180 rt_clip.top = 0;
3181 rt_clip.right = clnt.right;
3182 rt_clip.bottom = clnt.bottom;
3184 if (rt_scr.left < 0) rt_scr.left = 0;
3185 if (rt_clip.left < 0) rt_clip.left = 0;
3187 ScrollWindowEx(pane->hwnd, dx, 0, &rt_scr, &rt_clip, 0, 0, SW_INVALIDATE);
3189 rt_clip.right = pane->positions[idx+1];
3190 RedrawWindow(pane->hwnd, &rt_clip, 0, RDW_INVALIDATE|RDW_UPDATENOW);
3192 if (pnmh->code == HDN_ENDTRACKW) {
3193 SendMessageW(pane->hwnd, LB_SETHORIZONTALEXTENT, pane->positions[COLUMNS], 0);
3195 if (GetScrollPos(pane->hwnd, SB_HORZ) != scroll_pos)
3196 set_header(pane);
3200 return FALSE;
3203 case HDN_DIVIDERDBLCLICKW: {
3204 LPNMHEADERW phdn = (LPNMHEADERW)pnmh;
3205 HDITEMW item;
3207 calc_single_width(pane, phdn->iItem);
3208 item.mask = HDI_WIDTH;
3209 item.cxy = pane->widths[phdn->iItem];
3211 SendMessageW(pane->hwndHeader, HDM_SETITEMW, phdn->iItem, (LPARAM)&item);
3212 InvalidateRect(pane->hwnd, 0, TRUE);
3213 break;}
3216 return 0;
3219 static void scan_entry(ChildWnd* child, Entry* entry, int idx, HWND hwnd)
3221 WCHAR path[MAX_PATH];
3222 HCURSOR old_cursor = SetCursor(LoadCursorW(0, (LPCWSTR)IDC_WAIT));
3224 /* delete sub entries in left pane */
3225 for(;;) {
3226 LRESULT res = SendMessageW(child->left.hwnd, LB_GETITEMDATA, idx+1, 0);
3227 Entry* sub = (Entry*) res;
3229 if (res==LB_ERR || !sub || sub->level<=entry->level)
3230 break;
3232 SendMessageW(child->left.hwnd, LB_DELETESTRING, idx+1, 0);
3235 /* empty right pane */
3236 SendMessageW(child->right.hwnd, LB_RESETCONTENT, 0, 0);
3238 /* release memory */
3239 free_entries(entry);
3241 /* read contents from disk */
3242 if (entry->etype == ET_SHELL)
3244 read_directory(entry, NULL, child->sortOrder, hwnd);
3246 else
3248 get_path(entry, path);
3249 read_directory(entry, path, child->sortOrder, hwnd);
3252 /* insert found entries in right pane */
3253 insert_entries(&child->right, entry->down, child->filter_pattern, child->filter_flags, -1);
3254 calc_widths(&child->right, FALSE);
3255 set_header(&child->right);
3257 child->header_wdths_ok = FALSE;
3259 SetCursor(old_cursor);
3263 /* expand a directory entry */
3265 static BOOL expand_entry(ChildWnd* child, Entry* dir)
3267 int idx;
3268 Entry* p;
3270 if (!dir || dir->expanded || !dir->down)
3271 return FALSE;
3273 p = dir->down;
3275 if (p->data.cFileName[0]=='.' && p->data.cFileName[1]=='\0' && p->next) {
3276 p = p->next;
3278 if (p->data.cFileName[0]=='.' && p->data.cFileName[1]=='.' &&
3279 p->data.cFileName[2]=='\0' && p->next)
3280 p = p->next;
3283 /* no subdirectories ? */
3284 if (!(p->data.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY))
3285 return FALSE;
3287 idx = SendMessageW(child->left.hwnd, LB_FINDSTRING, 0, (LPARAM)dir);
3289 dir->expanded = TRUE;
3291 /* insert entries in left pane */
3292 insert_entries(&child->left, p, NULL, TF_ALL, idx);
3294 if (!child->header_wdths_ok) {
3295 if (calc_widths(&child->left, FALSE)) {
3296 set_header(&child->left);
3298 child->header_wdths_ok = TRUE;
3302 return TRUE;
3306 static void collapse_entry(Pane* pane, Entry* dir)
3308 int idx;
3310 if (!dir) return;
3311 idx = SendMessageW(pane->hwnd, LB_FINDSTRING, 0, (LPARAM)dir);
3313 ShowWindow(pane->hwnd, SW_HIDE);
3315 /* hide sub entries */
3316 for(;;) {
3317 LRESULT res = SendMessageW(pane->hwnd, LB_GETITEMDATA, idx+1, 0);
3318 Entry* sub = (Entry*) res;
3320 if (res==LB_ERR || !sub || sub->level<=dir->level)
3321 break;
3323 SendMessageW(pane->hwnd, LB_DELETESTRING, idx+1, 0);
3326 dir->expanded = FALSE;
3328 ShowWindow(pane->hwnd, SW_SHOW);
3332 static void refresh_right_pane(ChildWnd* child)
3334 SendMessageW(child->right.hwnd, LB_RESETCONTENT, 0, 0);
3335 insert_entries(&child->right, child->right.root, child->filter_pattern, child->filter_flags, -1);
3336 calc_widths(&child->right, FALSE);
3338 set_header(&child->right);
3341 static void set_curdir(ChildWnd* child, Entry* entry, int idx, HWND hwnd)
3343 WCHAR path[MAX_PATH];
3345 if (!entry)
3346 return;
3348 path[0] = '\0';
3350 child->left.cur = entry;
3352 child->right.root = entry->down? entry->down: entry;
3353 child->right.cur = entry;
3355 if (!entry->scanned)
3356 scan_entry(child, entry, idx, hwnd);
3357 else
3358 refresh_right_pane(child);
3360 get_path(entry, path);
3361 lstrcpyW(child->path, path);
3363 if (child->hwnd) /* only change window title, if the window already exists */
3364 SetWindowTextW(child->hwnd, path);
3366 if (path[0])
3367 if (SetCurrentDirectoryW(path))
3368 set_space_status();
3372 static void refresh_child(ChildWnd* child)
3374 WCHAR path[MAX_PATH], drv[_MAX_DRIVE+1];
3375 Entry* entry;
3376 int idx;
3378 get_path(child->left.cur, path);
3379 _wsplitpath(path, drv, NULL, NULL, NULL);
3381 child->right.root = NULL;
3383 scan_entry(child, &child->root.entry, 0, child->hwnd);
3385 if (child->root.entry.etype == ET_SHELL)
3387 LPITEMIDLIST local_pidl = get_path_pidl(path,child->hwnd);
3388 if (local_pidl)
3389 entry = read_tree(&child->root, NULL, local_pidl , drv, child->sortOrder, child->hwnd);
3390 else
3391 entry = NULL;
3393 else
3394 entry = read_tree(&child->root, path, NULL, drv, child->sortOrder, child->hwnd);
3396 if (!entry)
3397 entry = &child->root.entry;
3399 insert_entries(&child->left, child->root.entry.down, NULL, TF_ALL, 0);
3401 set_curdir(child, entry, 0, child->hwnd);
3403 idx = SendMessageW(child->left.hwnd, LB_FINDSTRING, 0, (LPARAM)child->left.cur);
3404 SendMessageW(child->left.hwnd, LB_SETCURSEL, idx, 0);
3408 static void create_drive_bar(void)
3410 TBBUTTON drivebarBtn = {0, 0, TBSTATE_ENABLED, BTNS_BUTTON, {0, 0}, 0, 0};
3411 WCHAR b1[BUFFER_LEN];
3412 int btn = 1;
3413 PWSTR p;
3415 GetLogicalDriveStringsW(BUFFER_LEN, Globals.drives);
3417 Globals.hdrivebar = CreateToolbarEx(Globals.hMainWnd, WS_CHILD|WS_VISIBLE|CCS_NOMOVEY|TBSTYLE_LIST,
3418 IDW_DRIVEBAR, 2, Globals.hInstance, IDB_DRIVEBAR, &drivebarBtn,
3419 0, 16, 13, 16, 13, sizeof(TBBUTTON));
3421 #ifdef __WINE__
3422 /* insert unix file system button */
3423 b1[0] = '/';
3424 b1[1] = '\0';
3425 b1[2] = '\0';
3426 SendMessageW(Globals.hdrivebar, TB_ADDSTRINGW, 0, (LPARAM)b1);
3428 drivebarBtn.idCommand = ID_DRIVE_UNIX_FS;
3429 SendMessageW(Globals.hdrivebar, TB_INSERTBUTTONW, btn++, (LPARAM)&drivebarBtn);
3430 drivebarBtn.iString++;
3431 #endif
3432 /* insert shell namespace button */
3433 load_string(b1, sizeof(b1)/sizeof(b1[0]), IDS_SHELL);
3434 b1[lstrlenW(b1)+1] = '\0';
3435 SendMessageW(Globals.hdrivebar, TB_ADDSTRINGW, 0, (LPARAM)b1);
3437 drivebarBtn.idCommand = ID_DRIVE_SHELL_NS;
3438 SendMessageW(Globals.hdrivebar, TB_INSERTBUTTONW, btn++, (LPARAM)&drivebarBtn);
3439 drivebarBtn.iString++;
3441 /* register windows drive root strings */
3442 SendMessageW(Globals.hdrivebar, TB_ADDSTRINGW, 0, (LPARAM)Globals.drives);
3444 drivebarBtn.idCommand = ID_DRIVE_FIRST;
3446 for(p=Globals.drives; *p; ) {
3447 switch(GetDriveTypeW(p)) {
3448 case DRIVE_REMOVABLE: drivebarBtn.iBitmap = 1; break;
3449 case DRIVE_CDROM: drivebarBtn.iBitmap = 3; break;
3450 case DRIVE_REMOTE: drivebarBtn.iBitmap = 4; break;
3451 case DRIVE_RAMDISK: drivebarBtn.iBitmap = 5; break;
3452 default:/*DRIVE_FIXED*/ drivebarBtn.iBitmap = 2;
3455 SendMessageW(Globals.hdrivebar, TB_INSERTBUTTONW, btn++, (LPARAM)&drivebarBtn);
3456 drivebarBtn.idCommand++;
3457 drivebarBtn.iString++;
3459 while(*p++);
3463 static void refresh_drives(void)
3465 RECT rect;
3467 /* destroy drive bar */
3468 DestroyWindow(Globals.hdrivebar);
3469 Globals.hdrivebar = 0;
3471 /* re-create drive bar */
3472 create_drive_bar();
3474 /* update window layout */
3475 GetClientRect(Globals.hMainWnd, &rect);
3476 SendMessageW(Globals.hMainWnd, WM_SIZE, 0, MAKELONG(rect.right, rect.bottom));
3480 static BOOL launch_file(HWND hwnd, LPCWSTR cmd, UINT nCmdShow)
3482 HINSTANCE hinst = ShellExecuteW(hwnd, NULL/*operation*/, cmd, NULL/*parameters*/, NULL/*dir*/, nCmdShow);
3484 if (PtrToUlong(hinst) <= 32) {
3485 display_error(hwnd, GetLastError());
3486 return FALSE;
3489 return TRUE;
3493 static BOOL launch_entry(Entry* entry, HWND hwnd, UINT nCmdShow)
3495 WCHAR cmd[MAX_PATH];
3497 if (entry->etype == ET_SHELL) {
3498 BOOL ret = TRUE;
3500 SHELLEXECUTEINFOW shexinfo;
3502 shexinfo.cbSize = sizeof(SHELLEXECUTEINFOW);
3503 shexinfo.fMask = SEE_MASK_IDLIST;
3504 shexinfo.hwnd = hwnd;
3505 shexinfo.lpVerb = NULL;
3506 shexinfo.lpFile = NULL;
3507 shexinfo.lpParameters = NULL;
3508 shexinfo.lpDirectory = NULL;
3509 shexinfo.nShow = nCmdShow;
3510 shexinfo.lpIDList = get_to_absolute_pidl(entry, hwnd);
3512 if (!ShellExecuteExW(&shexinfo)) {
3513 display_error(hwnd, GetLastError());
3514 ret = FALSE;
3517 if (shexinfo.lpIDList != entry->pidl)
3518 IMalloc_Free(Globals.iMalloc, shexinfo.lpIDList);
3520 return ret;
3523 get_path(entry, cmd);
3525 /* start program, open document... */
3526 return launch_file(hwnd, cmd, nCmdShow);
3530 static void activate_entry(ChildWnd* child, Pane* pane, HWND hwnd)
3532 Entry* entry = pane->cur;
3534 if (!entry)
3535 return;
3537 if (entry->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
3538 int scanned_old = entry->scanned;
3540 if (!scanned_old)
3542 int idx = SendMessageW(child->left.hwnd, LB_GETCURSEL, 0, 0);
3543 scan_entry(child, entry, idx, hwnd);
3546 if (entry->data.cFileName[0]=='.' && entry->data.cFileName[1]=='\0')
3547 return;
3549 if (entry->data.cFileName[0]=='.' && entry->data.cFileName[1]=='.' && entry->data.cFileName[2]=='\0') {
3550 entry = child->left.cur->up;
3551 collapse_entry(&child->left, entry);
3552 goto focus_entry;
3553 } else if (entry->expanded)
3554 collapse_entry(pane, child->left.cur);
3555 else {
3556 expand_entry(child, child->left.cur);
3558 if (!pane->treePane) focus_entry: {
3559 int idxstart = SendMessageW(child->left.hwnd, LB_GETCURSEL, 0, 0);
3560 int idx = SendMessageW(child->left.hwnd, LB_FINDSTRING, idxstart, (LPARAM)entry);
3561 SendMessageW(child->left.hwnd, LB_SETCURSEL, idx, 0);
3562 set_curdir(child, entry, idx, hwnd);
3566 if (!scanned_old) {
3567 calc_widths(pane, FALSE);
3569 set_header(pane);
3571 } else {
3572 if (GetKeyState(VK_MENU) < 0)
3573 show_properties_dlg(entry, child->hwnd);
3574 else
3575 launch_entry(entry, child->hwnd, SW_SHOWNORMAL);
3580 static BOOL pane_command(Pane* pane, UINT cmd)
3582 switch(cmd) {
3583 case ID_VIEW_NAME:
3584 if (pane->visible_cols) {
3585 pane->visible_cols = 0;
3586 calc_widths(pane, TRUE);
3587 set_header(pane);
3588 InvalidateRect(pane->hwnd, 0, TRUE);
3589 CheckMenuItem(Globals.hMenuView, ID_VIEW_NAME, MF_BYCOMMAND|MF_CHECKED);
3590 CheckMenuItem(Globals.hMenuView, ID_VIEW_ALL_ATTRIBUTES, MF_BYCOMMAND);
3592 break;
3594 case ID_VIEW_ALL_ATTRIBUTES:
3595 if (pane->visible_cols != COL_ALL) {
3596 pane->visible_cols = COL_ALL;
3597 calc_widths(pane, TRUE);
3598 set_header(pane);
3599 InvalidateRect(pane->hwnd, 0, TRUE);
3600 CheckMenuItem(Globals.hMenuView, ID_VIEW_NAME, MF_BYCOMMAND);
3601 CheckMenuItem(Globals.hMenuView, ID_VIEW_ALL_ATTRIBUTES, MF_BYCOMMAND|MF_CHECKED);
3603 break;
3605 case ID_PREFERRED_SIZES: {
3606 calc_widths(pane, TRUE);
3607 set_header(pane);
3608 InvalidateRect(pane->hwnd, 0, TRUE);
3609 break;}
3611 /* TODO: more command ids... */
3613 default:
3614 return FALSE;
3617 return TRUE;
3621 static void set_sort_order(ChildWnd* child, SORT_ORDER sortOrder)
3623 if (child->sortOrder != sortOrder) {
3624 child->sortOrder = sortOrder;
3625 refresh_child(child);
3629 static void update_view_menu(ChildWnd* child)
3631 CheckMenuItem(Globals.hMenuView, ID_VIEW_SORT_NAME, child->sortOrder==SORT_NAME? MF_CHECKED: MF_UNCHECKED);
3632 CheckMenuItem(Globals.hMenuView, ID_VIEW_SORT_TYPE, child->sortOrder==SORT_EXT? MF_CHECKED: MF_UNCHECKED);
3633 CheckMenuItem(Globals.hMenuView, ID_VIEW_SORT_SIZE, child->sortOrder==SORT_SIZE? MF_CHECKED: MF_UNCHECKED);
3634 CheckMenuItem(Globals.hMenuView, ID_VIEW_SORT_DATE, child->sortOrder==SORT_DATE? MF_CHECKED: MF_UNCHECKED);
3638 static BOOL is_directory(LPCWSTR target)
3640 /*TODO correctly handle UNIX paths */
3641 DWORD target_attr = GetFileAttributesW(target);
3643 if (target_attr == INVALID_FILE_ATTRIBUTES)
3644 return FALSE;
3646 return (target_attr & FILE_ATTRIBUTE_DIRECTORY) != 0;
3649 static BOOL prompt_target(Pane* pane, LPWSTR source, LPWSTR target)
3651 WCHAR path[MAX_PATH];
3652 int len;
3654 get_path(pane->cur, path);
3656 if (DialogBoxParamW(Globals.hInstance, MAKEINTRESOURCEW(IDD_SELECT_DESTINATION), pane->hwnd, DestinationDlgProc, (LPARAM)path) != IDOK)
3657 return FALSE;
3659 get_path(pane->cur, source);
3661 /* convert relative targets to absolute paths */
3662 if (path[0]!='/' && path[1]!=':') {
3663 get_path(pane->cur->up, target);
3664 len = lstrlenW(target);
3666 if (target[len-1]!='\\' && target[len-1]!='/')
3667 target[len++] = '/';
3669 lstrcpyW(target+len, path);
3670 } else
3671 lstrcpyW(target, path);
3673 /* If the target already exists as directory, create a new target below this. */
3674 if (is_directory(path)) {
3675 WCHAR fname[_MAX_FNAME], ext[_MAX_EXT];
3676 static const WCHAR sAppend[] = {'%','s','/','%','s','%','s','\0'};
3678 _wsplitpath(source, NULL, NULL, fname, ext);
3680 wsprintfW(target, sAppend, path, fname, ext);
3683 return TRUE;
3687 static IContextMenu2* s_pctxmenu2 = NULL;
3688 static IContextMenu3* s_pctxmenu3 = NULL;
3690 static void CtxMenu_reset(void)
3692 s_pctxmenu2 = NULL;
3693 s_pctxmenu3 = NULL;
3696 static IContextMenu* CtxMenu_query_interfaces(IContextMenu* pcm1)
3698 IContextMenu* pcm = NULL;
3700 CtxMenu_reset();
3702 if (IContextMenu_QueryInterface(pcm1, &IID_IContextMenu3, (void**)&pcm) == NOERROR)
3703 s_pctxmenu3 = (LPCONTEXTMENU3)pcm;
3704 else if (IContextMenu_QueryInterface(pcm1, &IID_IContextMenu2, (void**)&pcm) == NOERROR)
3705 s_pctxmenu2 = (LPCONTEXTMENU2)pcm;
3707 if (pcm) {
3708 IContextMenu_Release(pcm1);
3709 return pcm;
3710 } else
3711 return pcm1;
3714 static BOOL CtxMenu_HandleMenuMsg(UINT nmsg, WPARAM wparam, LPARAM lparam)
3716 if (s_pctxmenu3) {
3717 if (SUCCEEDED(IContextMenu3_HandleMenuMsg(s_pctxmenu3, nmsg, wparam, lparam)))
3718 return TRUE;
3721 if (s_pctxmenu2)
3722 if (SUCCEEDED(IContextMenu2_HandleMenuMsg(s_pctxmenu2, nmsg, wparam, lparam)))
3723 return TRUE;
3725 return FALSE;
3728 static HRESULT ShellFolderContextMenu(IShellFolder* shell_folder, HWND hwndParent, int cidl, LPCITEMIDLIST* apidl, int x, int y)
3730 IContextMenu* pcm;
3731 BOOL executed = FALSE;
3733 HRESULT hr = IShellFolder_GetUIObjectOf(shell_folder, hwndParent, cidl, apidl, &IID_IContextMenu, NULL, (LPVOID*)&pcm);
3735 if (SUCCEEDED(hr)) {
3736 HMENU hmenu = CreatePopupMenu();
3738 pcm = CtxMenu_query_interfaces(pcm);
3740 if (hmenu) {
3741 hr = IContextMenu_QueryContextMenu(pcm, hmenu, 0, FCIDM_SHVIEWFIRST, FCIDM_SHVIEWLAST, CMF_NORMAL);
3743 if (SUCCEEDED(hr)) {
3744 UINT idCmd = TrackPopupMenu(hmenu, TPM_LEFTALIGN|TPM_RETURNCMD|TPM_RIGHTBUTTON, x, y, 0, hwndParent, NULL);
3746 CtxMenu_reset();
3748 if (idCmd) {
3749 CMINVOKECOMMANDINFO cmi;
3751 cmi.cbSize = sizeof(CMINVOKECOMMANDINFO);
3752 cmi.fMask = 0;
3753 cmi.hwnd = hwndParent;
3754 cmi.lpVerb = (LPCSTR)(INT_PTR)(idCmd - FCIDM_SHVIEWFIRST);
3755 cmi.lpParameters = NULL;
3756 cmi.lpDirectory = NULL;
3757 cmi.nShow = SW_SHOWNORMAL;
3758 cmi.dwHotKey = 0;
3759 cmi.hIcon = 0;
3761 hr = IContextMenu_InvokeCommand(pcm, &cmi);
3762 executed = TRUE;
3764 } else
3765 CtxMenu_reset();
3768 IContextMenu_Release(pcm);
3771 return FAILED(hr)? hr: executed? S_OK: S_FALSE;
3774 static LRESULT CALLBACK ChildWndProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam)
3776 ChildWnd* child = (ChildWnd*)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
3777 ASSERT(child);
3779 switch(nmsg) {
3780 case WM_DRAWITEM: {
3781 LPDRAWITEMSTRUCT dis = (LPDRAWITEMSTRUCT)lparam;
3782 Entry* entry = (Entry*) dis->itemData;
3784 if (dis->CtlID == IDW_TREE_LEFT)
3785 draw_item(&child->left, dis, entry, -1);
3786 else if (dis->CtlID == IDW_TREE_RIGHT)
3787 draw_item(&child->right, dis, entry, -1);
3788 else
3789 goto draw_menu_item;
3791 return TRUE;}
3793 case WM_CREATE:
3794 InitChildWindow(child);
3795 break;
3797 case WM_NCDESTROY:
3798 free_child_window(child);
3799 SetWindowLongPtrW(hwnd, GWLP_USERDATA, 0);
3800 break;
3802 case WM_PAINT: {
3803 PAINTSTRUCT ps;
3804 HBRUSH lastBrush;
3805 RECT rt;
3806 GetClientRect(hwnd, &rt);
3807 BeginPaint(hwnd, &ps);
3808 rt.left = child->split_pos-SPLIT_WIDTH/2;
3809 rt.right = child->split_pos+SPLIT_WIDTH/2+1;
3810 lastBrush = SelectObject(ps.hdc, GetStockObject(COLOR_SPLITBAR));
3811 Rectangle(ps.hdc, rt.left, rt.top-1, rt.right, rt.bottom+1);
3812 SelectObject(ps.hdc, lastBrush);
3813 EndPaint(hwnd, &ps);
3814 break;}
3816 case WM_SETCURSOR:
3817 if (LOWORD(lparam) == HTCLIENT) {
3818 POINT pt;
3819 GetCursorPos(&pt);
3820 ScreenToClient(hwnd, &pt);
3822 if (pt.x>=child->split_pos-SPLIT_WIDTH/2 && pt.x<child->split_pos+SPLIT_WIDTH/2+1) {
3823 SetCursor(LoadCursorW(0, (LPCWSTR)IDC_SIZEWE));
3824 return TRUE;
3827 goto def;
3829 case WM_LBUTTONDOWN: {
3830 RECT rt;
3831 int x = (short)LOWORD(lparam);
3833 GetClientRect(hwnd, &rt);
3835 if (x>=child->split_pos-SPLIT_WIDTH/2 && x<child->split_pos+SPLIT_WIDTH/2+1) {
3836 last_split = child->split_pos;
3837 SetCapture(hwnd);
3840 break;}
3842 case WM_LBUTTONUP:
3843 if (GetCapture() == hwnd)
3844 ReleaseCapture();
3845 break;
3847 case WM_KEYDOWN:
3848 if (wparam == VK_ESCAPE)
3849 if (GetCapture() == hwnd) {
3850 RECT rt;
3851 child->split_pos = last_split;
3852 GetClientRect(hwnd, &rt);
3853 resize_tree(child, rt.right, rt.bottom);
3854 last_split = -1;
3855 ReleaseCapture();
3856 SetCursor(LoadCursorW(0, (LPCWSTR)IDC_ARROW));
3858 break;
3860 case WM_MOUSEMOVE:
3861 if (GetCapture() == hwnd) {
3862 RECT rt;
3863 int x = (short)LOWORD(lparam);
3865 GetClientRect(hwnd, &rt);
3867 if (x>=0 && x<rt.right) {
3868 child->split_pos = x;
3869 resize_tree(child, rt.right, rt.bottom);
3870 rt.left = x-SPLIT_WIDTH/2;
3871 rt.right = x+SPLIT_WIDTH/2+1;
3872 InvalidateRect(hwnd, &rt, FALSE);
3873 UpdateWindow(child->left.hwnd);
3874 UpdateWindow(hwnd);
3875 UpdateWindow(child->right.hwnd);
3878 break;
3880 case WM_GETMINMAXINFO:
3881 DefMDIChildProcW(hwnd, nmsg, wparam, lparam);
3883 {LPMINMAXINFO lpmmi = (LPMINMAXINFO)lparam;
3885 lpmmi->ptMaxTrackSize.x <<= 1;/*2*GetSystemMetrics(SM_CXSCREEN) / SM_CXVIRTUALSCREEN */
3886 lpmmi->ptMaxTrackSize.y <<= 1;/*2*GetSystemMetrics(SM_CYSCREEN) / SM_CYVIRTUALSCREEN */
3887 break;}
3889 case WM_SETFOCUS:
3890 if (SetCurrentDirectoryW(child->path))
3891 set_space_status();
3892 SetFocus(child->focus_pane? child->right.hwnd: child->left.hwnd);
3893 break;
3895 case WM_DISPATCH_COMMAND: {
3896 Pane* pane = GetFocus()==child->left.hwnd? &child->left: &child->right;
3898 switch(LOWORD(wparam)) {
3899 case ID_WINDOW_NEW: {
3900 ChildWnd* new_child = alloc_child_window(child->path, NULL, hwnd);
3902 if (!create_child_window(new_child))
3903 HeapFree(GetProcessHeap(), 0, new_child);
3905 break;}
3907 case ID_REFRESH:
3908 refresh_drives();
3909 refresh_child(child);
3910 break;
3912 case ID_ACTIVATE:
3913 activate_entry(child, pane, hwnd);
3914 break;
3916 case ID_FILE_MOVE: {
3917 WCHAR source[BUFFER_LEN], target[BUFFER_LEN];
3919 if (prompt_target(pane, source, target)) {
3920 SHFILEOPSTRUCTW shfo = {hwnd, FO_MOVE, source, target};
3922 source[lstrlenW(source)+1] = '\0';
3923 target[lstrlenW(target)+1] = '\0';
3925 if (!SHFileOperationW(&shfo))
3926 refresh_child(child);
3928 break;}
3930 case ID_FILE_COPY: {
3931 WCHAR source[BUFFER_LEN], target[BUFFER_LEN];
3933 if (prompt_target(pane, source, target)) {
3934 SHFILEOPSTRUCTW shfo = {hwnd, FO_COPY, source, target};
3936 source[lstrlenW(source)+1] = '\0';
3937 target[lstrlenW(target)+1] = '\0';
3939 if (!SHFileOperationW(&shfo))
3940 refresh_child(child);
3942 break;}
3944 case ID_FILE_DELETE: {
3945 WCHAR path[BUFFER_LEN];
3946 SHFILEOPSTRUCTW shfo = {hwnd, FO_DELETE, path, NULL, FOF_ALLOWUNDO};
3948 get_path(pane->cur, path);
3950 path[lstrlenW(path)+1] = '\0';
3952 if (!SHFileOperationW(&shfo))
3953 refresh_child(child);
3954 break;}
3956 case ID_VIEW_SORT_NAME:
3957 set_sort_order(child, SORT_NAME);
3958 break;
3960 case ID_VIEW_SORT_TYPE:
3961 set_sort_order(child, SORT_EXT);
3962 break;
3964 case ID_VIEW_SORT_SIZE:
3965 set_sort_order(child, SORT_SIZE);
3966 break;
3968 case ID_VIEW_SORT_DATE:
3969 set_sort_order(child, SORT_DATE);
3970 break;
3972 case ID_VIEW_FILTER: {
3973 struct FilterDialog dlg;
3975 memset(&dlg, 0, sizeof(struct FilterDialog));
3976 lstrcpyW(dlg.pattern, child->filter_pattern);
3977 dlg.flags = child->filter_flags;
3979 if (DialogBoxParamW(Globals.hInstance, MAKEINTRESOURCEW(IDD_DIALOG_VIEW_TYPE), hwnd, FilterDialogDlgProc, (LPARAM)&dlg) == IDOK) {
3980 lstrcpyW(child->filter_pattern, dlg.pattern);
3981 child->filter_flags = dlg.flags;
3982 refresh_right_pane(child);
3984 break;}
3986 case ID_VIEW_SPLIT: {
3987 last_split = child->split_pos;
3988 SetCapture(hwnd);
3989 break;}
3991 case ID_EDIT_PROPERTIES:
3992 show_properties_dlg(pane->cur, child->hwnd);
3993 break;
3995 default:
3996 return pane_command(pane, LOWORD(wparam));
3999 return TRUE;}
4001 case WM_COMMAND: {
4002 Pane* pane = GetFocus()==child->left.hwnd? &child->left: &child->right;
4004 switch(HIWORD(wparam)) {
4005 case LBN_SELCHANGE: {
4006 int idx = SendMessageW(pane->hwnd, LB_GETCURSEL, 0, 0);
4007 Entry* entry = (Entry*)SendMessageW(pane->hwnd, LB_GETITEMDATA, idx, 0);
4009 if (pane == &child->left)
4010 set_curdir(child, entry, idx, hwnd);
4011 else
4012 pane->cur = entry;
4013 break;}
4015 case LBN_DBLCLK:
4016 activate_entry(child, pane, hwnd);
4017 break;
4019 break;}
4021 case WM_NOTIFY: {
4022 NMHDR* pnmh = (NMHDR*) lparam;
4023 return pane_notify(pnmh->idFrom==IDW_HEADER_LEFT? &child->left: &child->right, pnmh);}
4025 case WM_CONTEXTMENU: {
4026 POINT pt, pt_clnt;
4027 Pane* pane;
4028 int idx;
4030 /* first select the current item in the listbox */
4031 HWND hpanel = (HWND) wparam;
4032 pt_clnt.x = pt.x = (short)LOWORD(lparam);
4033 pt_clnt.y = pt.y = (short)HIWORD(lparam);
4034 ScreenToClient(hpanel, &pt_clnt);
4035 SendMessageW(hpanel, WM_LBUTTONDOWN, 0, MAKELONG(pt_clnt.x, pt_clnt.y));
4036 SendMessageW(hpanel, WM_LBUTTONUP, 0, MAKELONG(pt_clnt.x, pt_clnt.y));
4038 /* now create the popup menu using shell namespace and IContextMenu */
4039 pane = GetFocus()==child->left.hwnd? &child->left: &child->right;
4040 idx = SendMessageW(pane->hwnd, LB_GETCURSEL, 0, 0);
4042 if (idx != -1) {
4043 Entry* entry = (Entry*)SendMessageW(pane->hwnd, LB_GETITEMDATA, idx, 0);
4045 LPITEMIDLIST pidl_abs = get_to_absolute_pidl(entry, hwnd);
4047 if (pidl_abs) {
4048 IShellFolder* parentFolder;
4049 LPCITEMIDLIST pidlLast;
4051 /* get and use the parent folder to display correct context menu in all cases */
4052 if (SUCCEEDED(SHBindToParent(pidl_abs, &IID_IShellFolder, (LPVOID*)&parentFolder, &pidlLast))) {
4053 if (ShellFolderContextMenu(parentFolder, hwnd, 1, &pidlLast, pt.x, pt.y) == S_OK)
4054 refresh_child(child);
4056 IShellFolder_Release(parentFolder);
4059 IMalloc_Free(Globals.iMalloc, pidl_abs);
4062 break;}
4064 case WM_MEASUREITEM:
4065 draw_menu_item:
4066 if (!wparam) /* Is the message menu-related? */
4067 if (CtxMenu_HandleMenuMsg(nmsg, wparam, lparam))
4068 return TRUE;
4070 break;
4072 case WM_INITMENUPOPUP:
4073 if (CtxMenu_HandleMenuMsg(nmsg, wparam, lparam))
4074 return 0;
4076 update_view_menu(child);
4077 break;
4079 case WM_MENUCHAR: /* only supported by IContextMenu3 */
4080 if (s_pctxmenu3) {
4081 LRESULT lResult = 0;
4083 IContextMenu3_HandleMenuMsg2(s_pctxmenu3, nmsg, wparam, lparam, &lResult);
4085 return lResult;
4088 break;
4090 case WM_SIZE:
4091 if (wparam != SIZE_MINIMIZED)
4092 resize_tree(child, LOWORD(lparam), HIWORD(lparam));
4093 /* fall through */
4095 default: def:
4096 return DefMDIChildProcW(hwnd, nmsg, wparam, lparam);
4099 return 0;
4103 static LRESULT CALLBACK TreeWndProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam)
4105 ChildWnd* child = (ChildWnd*)GetWindowLongPtrW(GetParent(hwnd), GWLP_USERDATA);
4106 Pane* pane = (Pane*)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
4107 ASSERT(child);
4109 switch(nmsg) {
4110 case WM_HSCROLL:
4111 set_header(pane);
4112 break;
4114 case WM_SETFOCUS:
4115 child->focus_pane = pane==&child->right? 1: 0;
4116 SendMessageW(hwnd, LB_SETSEL, TRUE, 1);
4117 /*TODO: check menu items */
4118 break;
4120 case WM_KEYDOWN:
4121 if (wparam == VK_TAB) {
4122 /*TODO: SetFocus(Globals.hdrivebar) */
4123 SetFocus(child->focus_pane? child->left.hwnd: child->right.hwnd);
4127 return CallWindowProcW(g_orgTreeWndProc, hwnd, nmsg, wparam, lparam);
4131 static void InitInstance(HINSTANCE hinstance)
4133 static const WCHAR sFont[] = {'M','i','c','r','o','s','o','f','t',' ','S','a','n','s',' ','S','e','r','i','f','\0'};
4135 WNDCLASSEXW wcFrame;
4136 WNDCLASSW wcChild;
4137 int col;
4139 INITCOMMONCONTROLSEX icc = {
4140 sizeof(INITCOMMONCONTROLSEX),
4141 ICC_BAR_CLASSES
4144 HDC hdc = GetDC(0);
4146 setlocale(LC_COLLATE, ""); /* set collating rules to local settings for compareName */
4148 InitCommonControlsEx(&icc);
4151 /* register frame window class */
4153 wcFrame.cbSize = sizeof(WNDCLASSEXW);
4154 wcFrame.style = 0;
4155 wcFrame.lpfnWndProc = FrameWndProc;
4156 wcFrame.cbClsExtra = 0;
4157 wcFrame.cbWndExtra = 0;
4158 wcFrame.hInstance = hinstance;
4159 wcFrame.hIcon = LoadIconW(hinstance, MAKEINTRESOURCEW(IDI_WINEFILE));
4160 wcFrame.hCursor = LoadCursorW(0, (LPCWSTR)IDC_ARROW);
4161 wcFrame.hbrBackground = 0;
4162 wcFrame.lpszMenuName = 0;
4163 wcFrame.lpszClassName = sWINEFILEFRAME;
4164 wcFrame.hIconSm = LoadImageW(hinstance, MAKEINTRESOURCEW(IDI_WINEFILE), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED);
4166 Globals.hframeClass = RegisterClassExW(&wcFrame);
4169 /* register tree windows class */
4171 wcChild.style = CS_CLASSDC|CS_DBLCLKS|CS_VREDRAW;
4172 wcChild.lpfnWndProc = ChildWndProc;
4173 wcChild.cbClsExtra = 0;
4174 wcChild.cbWndExtra = 0;
4175 wcChild.hInstance = hinstance;
4176 wcChild.hIcon = LoadIconW(hinstance, MAKEINTRESOURCEW(IDI_WINEFILE));
4177 wcChild.hCursor = LoadCursorW(0, (LPCWSTR)IDC_ARROW);
4178 wcChild.hbrBackground = 0;
4179 wcChild.lpszMenuName = 0;
4180 wcChild.lpszClassName = sWINEFILETREE;
4182 RegisterClassW(&wcChild);
4185 Globals.haccel = LoadAcceleratorsW(hinstance, MAKEINTRESOURCEW(IDA_WINEFILE));
4187 Globals.hfont = CreateFontW(-MulDiv(8,GetDeviceCaps(hdc,LOGPIXELSY),72), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, sFont);
4189 ReleaseDC(0, hdc);
4191 Globals.hInstance = hinstance;
4193 CoInitialize(NULL);
4194 CoGetMalloc(MEMCTX_TASK, &Globals.iMalloc);
4195 SHGetDesktopFolder(&Globals.iDesktop);
4196 Globals.cfStrFName = RegisterClipboardFormatW(CFSTR_FILENAMEW);
4198 /* load column strings */
4199 col = 1;
4201 load_string(g_pos_names[col++], sizeof(g_pos_names[col])/sizeof(g_pos_names[col][0]), IDS_COL_NAME);
4202 load_string(g_pos_names[col++], sizeof(g_pos_names[col])/sizeof(g_pos_names[col][0]), IDS_COL_SIZE);
4203 load_string(g_pos_names[col++], sizeof(g_pos_names[col])/sizeof(g_pos_names[col][0]), IDS_COL_CDATE);
4204 load_string(g_pos_names[col++], sizeof(g_pos_names[col])/sizeof(g_pos_names[col][0]), IDS_COL_ADATE);
4205 load_string(g_pos_names[col++], sizeof(g_pos_names[col])/sizeof(g_pos_names[col][0]), IDS_COL_MDATE);
4206 load_string(g_pos_names[col++], sizeof(g_pos_names[col])/sizeof(g_pos_names[col][0]), IDS_COL_IDX);
4207 load_string(g_pos_names[col++], sizeof(g_pos_names[col])/sizeof(g_pos_names[col][0]), IDS_COL_LINKS);
4208 load_string(g_pos_names[col++], sizeof(g_pos_names[col])/sizeof(g_pos_names[col][0]), IDS_COL_ATTR);
4209 load_string(g_pos_names[col++], sizeof(g_pos_names[col])/sizeof(g_pos_names[col][0]), IDS_COL_SEC);
4213 static BOOL show_frame(HWND hwndParent, int cmdshow, LPCWSTR path)
4215 static const WCHAR sMDICLIENT[] = {'M','D','I','C','L','I','E','N','T','\0'};
4217 WCHAR buffer[MAX_PATH], b1[BUFFER_LEN];
4218 ChildWnd* child;
4219 HMENU hMenuFrame, hMenuWindow;
4220 windowOptions opts;
4222 CLIENTCREATESTRUCT ccs;
4224 if (Globals.hMainWnd)
4225 return TRUE;
4227 opts = load_registry_settings();
4228 hMenuFrame = LoadMenuW(Globals.hInstance, MAKEINTRESOURCEW(IDM_WINEFILE));
4229 hMenuWindow = GetSubMenu(hMenuFrame, GetMenuItemCount(hMenuFrame)-2);
4231 Globals.hMenuFrame = hMenuFrame;
4232 Globals.hMenuView = GetSubMenu(hMenuFrame, 2);
4233 Globals.hMenuOptions = GetSubMenu(hMenuFrame, 3);
4235 ccs.hWindowMenu = hMenuWindow;
4236 ccs.idFirstChild = IDW_FIRST_CHILD;
4239 /* create main window */
4240 Globals.hMainWnd = CreateWindowExW(0, MAKEINTRESOURCEW(Globals.hframeClass), RS(b1,IDS_WINEFILE), WS_OVERLAPPEDWINDOW,
4241 opts.start_x, opts.start_y, opts.width, opts.height,
4242 hwndParent, Globals.hMenuFrame, Globals.hInstance, 0/*lpParam*/);
4245 Globals.hmdiclient = CreateWindowExW(0, sMDICLIENT, NULL,
4246 WS_CHILD|WS_CLIPCHILDREN|WS_VSCROLL|WS_HSCROLL|WS_VISIBLE|WS_BORDER,
4247 0, 0, 0, 0,
4248 Globals.hMainWnd, 0, Globals.hInstance, &ccs);
4250 CheckMenuItem(Globals.hMenuOptions, ID_VIEW_DRIVE_BAR, MF_BYCOMMAND|MF_CHECKED);
4251 CheckMenuItem(Globals.hMenuOptions, ID_VIEW_SAVESETTINGS, MF_BYCOMMAND);
4253 create_drive_bar();
4256 TBBUTTON toolbarBtns[] = {
4257 {0, 0, 0, BTNS_SEP, {0, 0}, 0, 0},
4258 {0, ID_WINDOW_NEW, TBSTATE_ENABLED, BTNS_BUTTON, {0, 0}, 0, 0},
4259 {1, ID_WINDOW_CASCADE, TBSTATE_ENABLED, BTNS_BUTTON, {0, 0}, 0, 0},
4260 {2, ID_WINDOW_TILE_HORZ, TBSTATE_ENABLED, BTNS_BUTTON, {0, 0}, 0, 0},
4261 {3, ID_WINDOW_TILE_VERT, TBSTATE_ENABLED, BTNS_BUTTON, {0, 0}, 0, 0},
4264 Globals.htoolbar = CreateToolbarEx(Globals.hMainWnd, WS_CHILD|WS_VISIBLE,
4265 IDW_TOOLBAR, 2, Globals.hInstance, IDB_TOOLBAR, toolbarBtns,
4266 sizeof(toolbarBtns)/sizeof(TBBUTTON), 16, 15, 16, 15, sizeof(TBBUTTON));
4267 CheckMenuItem(Globals.hMenuOptions, ID_VIEW_TOOL_BAR, MF_BYCOMMAND|MF_CHECKED);
4270 Globals.hstatusbar = CreateStatusWindowW(WS_CHILD|WS_VISIBLE, 0, Globals.hMainWnd, IDW_STATUSBAR);
4271 CheckMenuItem(Globals.hMenuOptions, ID_VIEW_STATUSBAR, MF_BYCOMMAND|MF_CHECKED);
4273 /*TODO: read paths from registry */
4275 if (!path || !*path) {
4276 GetCurrentDirectoryW(MAX_PATH, buffer);
4277 path = buffer;
4280 ShowWindow(Globals.hMainWnd, cmdshow);
4282 #ifndef __WINE__
4283 /* Shell Namespace as default: */
4284 child = alloc_child_window(path, get_path_pidl(path,Globals.hMainWnd), Globals.hMainWnd);
4285 #else
4286 child = alloc_child_window(path, NULL, Globals.hMainWnd);
4287 #endif
4289 child->pos.showCmd = SW_SHOWMAXIMIZED;
4290 child->pos.rcNormalPosition.left = 0;
4291 child->pos.rcNormalPosition.top = 0;
4292 child->pos.rcNormalPosition.right = 320;
4293 child->pos.rcNormalPosition.bottom = 280;
4295 if (!create_child_window(child)) {
4296 HeapFree(GetProcessHeap(), 0, child);
4297 return FALSE;
4300 SetWindowPlacement(child->hwnd, &child->pos);
4302 Globals.himl = ImageList_LoadImageW(Globals.hInstance, MAKEINTRESOURCEW(IDB_IMAGES), 16, 0, RGB(0,255,0), IMAGE_BITMAP, 0);
4304 Globals.prescan_node = FALSE;
4306 UpdateWindow(Globals.hMainWnd);
4308 if (child->hwnd && path && path[0])
4310 int index,count;
4311 WCHAR drv[_MAX_DRIVE+1], dir[_MAX_DIR], name[_MAX_FNAME], ext[_MAX_EXT];
4312 WCHAR fullname[_MAX_FNAME+_MAX_EXT+1];
4314 memset(name,0,sizeof(name));
4315 memset(name,0,sizeof(ext));
4316 _wsplitpath(path, drv, dir, name, ext);
4317 if (name[0])
4319 count = SendMessageW(child->right.hwnd, LB_GETCOUNT, 0, 0);
4320 lstrcpyW(fullname,name);
4321 lstrcatW(fullname,ext);
4323 for (index = 0; index < count; index ++)
4325 Entry* entry = (Entry*)SendMessageW(child->right.hwnd, LB_GETITEMDATA, index, 0);
4326 if (lstrcmpW(entry->data.cFileName,fullname)==0 ||
4327 lstrcmpW(entry->data.cAlternateFileName,fullname)==0)
4329 SendMessageW(child->right.hwnd, LB_SETCURSEL, index, 0);
4330 SetFocus(child->right.hwnd);
4331 break;
4336 return TRUE;
4339 static void ExitInstance(void)
4341 IShellFolder_Release(Globals.iDesktop);
4342 IMalloc_Release(Globals.iMalloc);
4343 CoUninitialize();
4345 DeleteObject(Globals.hfont);
4346 ImageList_Destroy(Globals.himl);
4349 int APIENTRY wWinMain(HINSTANCE hinstance, HINSTANCE previnstance, LPWSTR cmdline, int cmdshow)
4351 MSG msg;
4353 InitInstance(hinstance);
4355 if( !show_frame(0, cmdshow, cmdline) )
4357 ExitInstance();
4358 return 1;
4361 while(GetMessageW(&msg, 0, 0, 0)) {
4362 if (Globals.hmdiclient && TranslateMDISysAccel(Globals.hmdiclient, &msg))
4363 continue;
4365 if (Globals.hMainWnd && TranslateAcceleratorW(Globals.hMainWnd, Globals.haccel, &msg))
4366 continue;
4368 TranslateMessage(&msg);
4369 DispatchMessageW(&msg);
4372 ExitInstance();
4374 return msg.wParam;