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