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