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