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