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