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