More safe array tests.
[wine] / programs / winefile / winefile.c
1 /*
2  * Winefile
3  *
4  * Copyright 2000 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 #endif
25
26 #include <locale.h>
27
28 #define NONAMELESSUNION
29 #include "winefile.h"
30 #include "resource.h"
31
32 /* for read_directory_unix() */
33 #if !defined(_NO_EXTENSIONS) && defined(__WINE__)
34 #include <dirent.h>
35 #include <sys/stat.h>
36 #ifdef HAVE_UNISTD_H
37 # include <unistd.h>
38 #endif
39 #include <time.h>
40 #endif
41
42 #ifdef _NO_EXTENSIONS
43 #undef _LEFT_FILES
44 #endif
45
46 #ifndef _MAX_PATH
47 #define _MAX_DRIVE          3
48 #define _MAX_FNAME          256
49 #define _MAX_DIR            _MAX_FNAME
50 #define _MAX_EXT            _MAX_FNAME
51 #define _MAX_PATH           260
52 #endif
53
54 #ifdef NONAMELESSUNION
55 #define UNION_MEMBER(x) DUMMYUNIONNAME.x
56 #else
57 #define UNION_MEMBER(x) x
58 #endif
59
60
61 #ifdef _SHELL_FOLDERS
62 #define DEFAULT_SPLIT_POS       300;
63 #else
64 #define DEFAULT_SPLIT_POS       200;
65 #endif
66
67
68 WINEFILE_GLOBALS Globals;
69
70 extern void WineLicense(HWND hwnd);
71 extern void WineWarranty(HWND hwnd);
72
73 enum ENTRY_TYPE {
74         ET_WINDOWS,
75         ET_UNIX,
76 #ifdef _SHELL_FOLDERS
77         ET_SHELL
78 #endif
79 };
80
81 typedef struct _Entry {
82         struct _Entry*  next;
83         struct _Entry*  down;
84         struct _Entry*  up;
85
86         BOOL                    expanded;
87         BOOL                    scanned;
88         int                             level;
89
90         WIN32_FIND_DATA data;
91
92 #ifndef _NO_EXTENSIONS
93         BY_HANDLE_FILE_INFORMATION bhfi;
94         BOOL                    bhfi_valid;
95         enum ENTRY_TYPE etype;
96 #endif
97 #ifdef _SHELL_FOLDERS
98         LPITEMIDLIST    pidl;
99         IShellFolder*   folder;
100         HICON                   hicon;
101 #endif
102 } Entry;
103
104 typedef struct {
105         Entry   entry;
106         TCHAR   path[MAX_PATH];
107         TCHAR   volname[_MAX_FNAME];
108         TCHAR   fs[_MAX_DIR];
109         DWORD   drive_type;
110         DWORD   fs_flags;
111 } Root;
112
113 enum COLUMN_FLAGS {
114         COL_SIZE                = 0x01,
115         COL_DATE                = 0x02,
116         COL_TIME                = 0x04,
117         COL_ATTRIBUTES  = 0x08,
118         COL_DOSNAMES    = 0x10,
119 #ifdef _NO_EXTENSIONS
120         COL_ALL = COL_SIZE|COL_DATE|COL_TIME|COL_ATTRIBUTES|COL_DOSNAMES
121 #else
122         COL_INDEX               = 0x20,
123         COL_LINKS               = 0x40,
124         COL_ALL = COL_SIZE|COL_DATE|COL_TIME|COL_ATTRIBUTES|COL_DOSNAMES|COL_INDEX|COL_LINKS
125 #endif
126 };
127
128 typedef enum {
129         SORT_NAME,
130         SORT_EXT,
131         SORT_SIZE,
132         SORT_DATE
133 } SORT_ORDER;
134
135 typedef struct {
136         HWND    hwnd;
137 #ifndef _NO_EXTENSIONS
138         HWND    hwndHeader;
139 #endif
140
141 #ifndef _NO_EXTENSIONS
142 #define COLUMNS 10
143 #else
144 #define COLUMNS 5
145 #endif
146         int             widths[COLUMNS];
147         int             positions[COLUMNS+1];
148
149         BOOL    treePane;
150         int             visible_cols;
151         Entry*  root;
152         Entry*  cur;
153 } Pane;
154
155 typedef struct {
156         HWND    hwnd;
157         Pane    left;
158         Pane    right;
159         int             focus_pane;             /* 0: left  1: right */
160         WINDOWPLACEMENT pos;
161         int             split_pos;
162         BOOL    header_wdths_ok;
163
164         TCHAR   path[MAX_PATH];
165         Root    root;
166
167         SORT_ORDER sortOrder;
168 } ChildWnd;
169
170
171 static void read_directory(Entry* dir, LPCTSTR path, SORT_ORDER sortOrder, HWND hwnd);
172 static void set_curdir(ChildWnd* child, Entry* entry, HWND hwnd);
173 static void get_path(Entry* dir, PTSTR path);
174
175 LRESULT CALLBACK FrameWndProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam);
176 LRESULT CALLBACK ChildWndProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam);
177 LRESULT CALLBACK TreeWndProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam);
178
179
180 static void display_error(HWND hwnd, DWORD error)
181 {
182         PTSTR msg;
183
184         if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
185                 0, error, MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), (PTSTR)&msg, 0, NULL))
186                 MessageBox(hwnd, msg, TEXT("Winefile"), MB_OK);
187         else
188                 MessageBox(hwnd, TEXT("Error"), TEXT("Winefile"), MB_OK);
189
190         LocalFree(msg);
191 }
192
193
194 /* allocate and initialise a directory entry */
195 static Entry* alloc_entry()
196 {
197         Entry* entry = (Entry*) malloc(sizeof(Entry));
198
199 #ifdef _SHELL_FOLDERS
200         entry->pidl = NULL;
201         entry->folder = NULL;
202         entry->hicon = 0;
203 #endif
204
205         return entry;
206 }
207
208 /* free a directory entry */
209 static void free_entry(Entry* entry)
210 {
211 #ifdef _SHELL_FOLDERS
212         if (entry->hicon && entry->hicon!=(HICON)-1)
213                 DestroyIcon(entry->hicon);
214
215         if (entry->folder && entry->folder!=Globals.iDesktop)
216                 (*entry->folder->lpVtbl->Release)(entry->folder);
217
218         if (entry->pidl)
219                 (*Globals.iMalloc->lpVtbl->Free)(Globals.iMalloc, entry->pidl);
220 #endif
221
222         free(entry);
223 }
224
225 /* recursively free all child entries */
226 static void free_entries(Entry* dir)
227 {
228         Entry *entry, *next=dir->down;
229
230         if (next) {
231                 dir->down = 0;
232
233                 do {
234                         entry = next;
235                         next = entry->next;
236
237                         free_entries(entry);
238                         free_entry(entry);
239                 } while(next);
240         }
241 }
242
243
244 static void read_directory_win(Entry* dir, LPCTSTR path)
245 {
246         Entry* first_entry = NULL;
247         Entry* last = NULL;
248         Entry* entry;
249
250         int level = dir->level + 1;
251         WIN32_FIND_DATA w32fd;
252         HANDLE hFind;
253 #ifndef _NO_EXTENSIONS
254         HANDLE hFile;
255 #endif
256
257         TCHAR buffer[MAX_PATH], *p;
258         for(p=buffer; *path; )
259                 *p++ = *path++;
260
261         lstrcpy(p, TEXT("\\*"));
262
263         hFind = FindFirstFile(buffer, &w32fd);
264
265         if (hFind != INVALID_HANDLE_VALUE) {
266                 do {
267                         entry = alloc_entry();
268
269                         if (!first_entry)
270                                 first_entry = entry;
271
272                         if (last)
273                                 last->next = entry;
274
275                         memcpy(&entry->data, &w32fd, sizeof(WIN32_FIND_DATA));
276                         entry->down = NULL;
277                         entry->up = dir;
278                         entry->expanded = FALSE;
279                         entry->scanned = FALSE;
280                         entry->level = level;
281
282 #ifdef _NO_EXTENSIONS
283                         /* hide directory entry "." */
284                         if (entry->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
285                                 LPCTSTR name = entry->data.cFileName;
286
287                                 if (name[0]=='.' && name[1]=='\0')
288                                         continue;
289                         }
290 #else
291                         entry->etype = ET_WINDOWS;
292                         entry->bhfi_valid = FALSE;
293
294                         lstrcpy(p+1, entry->data.cFileName);
295
296                         hFile = CreateFile(buffer, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
297                                                                 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0);
298
299                         if (hFile != INVALID_HANDLE_VALUE) {
300                                 if (GetFileInformationByHandle(hFile, &entry->bhfi))
301                                         entry->bhfi_valid = TRUE;
302
303                                 CloseHandle(hFile);
304                         }
305 #endif
306
307                         last = entry;
308                 } while(FindNextFile(hFind, &entry->data));
309
310                 last->next = NULL;
311
312                 FindClose(hFind);
313         }
314
315         dir->down = first_entry;
316         dir->scanned = TRUE;
317 }
318
319
320 static Entry* find_entry_win(Entry* dir, LPCTSTR name)
321 {
322         Entry* entry;
323
324         for(entry=dir->down; entry; entry=entry->next) {
325                 LPCTSTR p = name;
326                 LPCTSTR q = entry->data.cFileName;
327
328                 do {
329                         if (!*p || *p==TEXT('\\') || *p==TEXT('/'))
330                                 return entry;
331                 } while(tolower(*p++) == tolower(*q++));
332
333                 p = name;
334                 q = entry->data.cAlternateFileName;
335
336                 do {
337                         if (!*p || *p==TEXT('\\') || *p==TEXT('/'))
338                                 return entry;
339                 } while(tolower(*p++) == tolower(*q++));
340         }
341
342         return 0;
343 }
344
345
346 static Entry* read_tree_win(Root* root, LPCTSTR path, SORT_ORDER sortOrder, HWND hwnd)
347 {
348         TCHAR buffer[MAX_PATH];
349         Entry* entry = &root->entry;
350         LPCTSTR s = path;
351         PTSTR d = buffer;
352
353         HCURSOR old_cursor = SetCursor(LoadCursor(0, IDC_WAIT));
354
355 #ifndef _NO_EXTENSIONS
356         entry->etype = ET_WINDOWS;
357 #endif
358
359         while(entry) {
360                 while(*s && *s!=TEXT('\\') && *s!=TEXT('/'))
361                         *d++ = *s++;
362
363                 while(*s==TEXT('\\') || *s==TEXT('/'))
364                         s++;
365
366                 *d++ = TEXT('\\');
367                 *d = TEXT('\0');
368
369                 read_directory(entry, buffer, sortOrder, hwnd);
370
371                 if (entry->down)
372                         entry->expanded = TRUE;
373
374                 if (!*s)
375                         break;
376
377                 entry = find_entry_win(entry, s);
378         }
379
380         SetCursor(old_cursor);
381
382         return entry;
383 }
384
385
386 #if !defined(_NO_EXTENSIONS) && defined(__WINE__)
387
388 BOOL time_to_filetime(const time_t* t, FILETIME* ftime)
389 {
390         struct tm* tm = gmtime(t);
391         SYSTEMTIME stime;
392
393         if (!tm)
394                 return FALSE;
395
396         stime.wYear = tm->tm_year+1900;
397         stime.wMonth = tm->tm_mon+1;
398         /*      stime.wDayOfWeek */
399         stime.wDay = tm->tm_mday;
400         stime.wHour = tm->tm_hour;
401         stime.wMinute = tm->tm_min;
402         stime.wSecond = tm->tm_sec;
403
404         return SystemTimeToFileTime(&stime, ftime);
405 }
406
407 static void read_directory_unix(Entry* dir, LPCTSTR path)
408 {
409         Entry* first_entry = NULL;
410         Entry* last = NULL;
411         Entry* entry;
412
413         int level = dir->level + 1;
414
415         DIR* pdir = opendir(path);
416
417         if (pdir) {
418                 struct stat st;
419                 struct dirent* ent;
420                 TCHAR buffer[MAX_PATH], *p;
421
422                 for(p=buffer; *path; )
423                         *p++ = *path++;
424
425                 if (p==buffer || p[-1]!='/')
426                         *p++ = '/';
427
428                 while((ent=readdir(pdir))) {
429                         entry = alloc_entry();
430
431                         if (!first_entry)
432                                 first_entry = entry;
433
434                         if (last)
435                                 last->next = entry;
436
437                         entry->etype = ET_UNIX;
438
439                         lstrcpy(entry->data.cFileName, ent->d_name);
440                         entry->data.dwFileAttributes = ent->d_name[0]=='.'? FILE_ATTRIBUTE_HIDDEN: 0;
441
442                         strcpy(p, ent->d_name);
443
444                         if (!stat(buffer, &st)) {
445                                 if (S_ISDIR(st.st_mode))
446                                         entry->data.dwFileAttributes |= FILE_ATTRIBUTE_DIRECTORY;
447
448                                 entry->data.nFileSizeLow = st.st_size & 0xFFFFFFFF;
449                                 entry->data.nFileSizeHigh = st.st_size >> 32;
450
451                                 memset(&entry->data.ftCreationTime, 0, sizeof(FILETIME));
452                                 time_to_filetime(&st.st_atime, &entry->data.ftLastAccessTime);
453                                 time_to_filetime(&st.st_mtime, &entry->data.ftLastWriteTime);
454
455                                 entry->bhfi.nFileIndexLow = ent->d_ino;
456                                 entry->bhfi.nFileIndexHigh = 0;
457
458                                 entry->bhfi.nNumberOfLinks = st.st_nlink;
459
460                                 entry->bhfi_valid = TRUE;
461                         } else {
462                                 entry->data.nFileSizeLow = 0;
463                                 entry->data.nFileSizeHigh = 0;
464                                 entry->bhfi_valid = FALSE;
465                         }
466
467                         entry->down = NULL;
468                         entry->up = dir;
469                         entry->expanded = FALSE;
470                         entry->scanned = FALSE;
471                         entry->level = level;
472
473                         last = entry;
474                 }
475
476                 last->next = NULL;
477
478                 closedir(pdir);
479         }
480
481         dir->down = first_entry;
482         dir->scanned = TRUE;
483 }
484
485 static Entry* find_entry_unix(Entry* dir, LPCTSTR name)
486 {
487         Entry* entry;
488
489         for(entry=dir->down; entry; entry=entry->next) {
490                 LPCTSTR p = name;
491                 LPCTSTR q = entry->data.cFileName;
492
493                 do {
494                         if (!*p || *p==TEXT('/'))
495                                 return entry;
496                 } while(*p++ == *q++);
497         }
498
499         return 0;
500 }
501
502 static Entry* read_tree_unix(Root* root, LPCTSTR path, SORT_ORDER sortOrder, HWND hwnd)
503 {
504         TCHAR buffer[MAX_PATH];
505         Entry* entry = &root->entry;
506         LPCTSTR s = path;
507         PTSTR d = buffer;
508
509         HCURSOR old_cursor = SetCursor(LoadCursor(0, IDC_WAIT));
510
511         entry->etype = ET_UNIX;
512
513         while(entry) {
514                 while(*s && *s!=TEXT('/'))
515                         *d++ = *s++;
516
517                 while(*s == TEXT('/'))
518                         s++;
519
520                 *d++ = TEXT('/');
521                 *d = TEXT('\0');
522
523                 read_directory(entry, buffer, sortOrder, hwnd);
524
525                 if (entry->down)
526                         entry->expanded = TRUE;
527
528                 if (!*s)
529                         break;
530
531                 entry = find_entry_unix(entry, s);
532         }
533
534         SetCursor(old_cursor);
535
536         return entry;
537 }
538
539 #endif /* !defined(_NO_EXTENSIONS) && defined(__WINE__) */
540
541
542 #ifdef _SHELL_FOLDERS
543
544 #ifdef UNICODE
545 #define tcscpyn strcpyn
546 #define get_strret get_strretW
547 #define path_from_pidl path_from_pidlW
548 #else
549 #define tcscpyn wcscpyn
550 #define get_strret get_strretA
551 #define path_from_pidl path_from_pidlA
552 #endif
553
554
555 static LPSTR strcpyn(LPSTR dest, LPCSTR source, size_t count)
556 {
557  LPCSTR s;
558  LPSTR d = dest;
559
560  for(s=source; count&&(*d++=*s++); )
561   count--;
562
563  return dest;
564 }
565
566 static LPWSTR wcscpyn(LPWSTR dest, LPCWSTR source, size_t count)
567 {
568  LPCWSTR s;
569  LPWSTR d = dest;
570
571  for(s=source; count&&(*d++=*s++); )
572   count--;
573
574  return dest;
575 }
576
577
578 static void get_strretA(STRRET* str, const SHITEMID* shiid, LPSTR buffer, int len)
579 {
580  switch(str->uType) {
581   case STRRET_WSTR:
582         WideCharToMultiByte(CP_ACP, 0, str->UNION_MEMBER(pOleStr), -1, buffer, len, NULL, NULL);
583         break;
584
585   case STRRET_OFFSET:
586         strcpyn(buffer, (LPCSTR)shiid+str->UNION_MEMBER(uOffset), len);
587         break;
588
589   case STRRET_CSTR:
590         strcpyn(buffer, str->UNION_MEMBER(cStr), len);
591  }
592 }
593
594 static void get_strretW(STRRET* str, const SHITEMID* shiid, LPWSTR buffer, int len)
595 {
596  switch(str->uType) {
597   case STRRET_WSTR:
598         wcscpyn(buffer, str->UNION_MEMBER(pOleStr), len);
599         break;
600
601   case STRRET_OFFSET:
602         MultiByteToWideChar(CP_ACP, 0, (LPCSTR)shiid+str->UNION_MEMBER(uOffset), -1, buffer, len);
603         break;
604
605   case STRRET_CSTR:
606         MultiByteToWideChar(CP_ACP, 0, str->UNION_MEMBER(cStr), -1, buffer, len);
607  }
608 }
609
610
611 static void free_strret(STRRET* str)
612 {
613         if (str->uType == STRRET_WSTR)
614                 (*Globals.iMalloc->lpVtbl->Free)(Globals.iMalloc, str->UNION_MEMBER(pOleStr));
615 }
616
617
618 HRESULT name_from_pidl(IShellFolder* folder, LPITEMIDLIST pidl, LPTSTR buffer, int len, SHGDNF flags)
619 {
620         STRRET str;
621
622         HRESULT hr = (*folder->lpVtbl->GetDisplayNameOf)(folder, pidl, flags, &str);
623
624         if (SUCCEEDED(hr)) {
625                 get_strret(&str, &pidl->mkid, buffer, len);
626                 free_strret(&str);
627         } else
628                 buffer[0] = '\0';
629
630         return hr;
631 }
632
633
634 HRESULT path_from_pidlA(IShellFolder* folder, LPITEMIDLIST pidl, LPSTR buffer, int len)
635 {
636         STRRET str;
637
638          /* SHGDN_FORPARSING: get full path of id list */
639         HRESULT hr = (*folder->lpVtbl->GetDisplayNameOf)(folder, pidl, SHGDN_FORPARSING, &str);
640
641         if (SUCCEEDED(hr)) {
642                 get_strretA(&str, &pidl->mkid, buffer, len);
643                 free_strret(&str);
644         } else
645                 buffer[0] = '\0';
646
647         return hr;
648 }
649
650 HRESULT path_from_pidlW(IShellFolder* folder, LPITEMIDLIST pidl, LPWSTR buffer, int len)
651 {
652         STRRET str;
653
654          /* SHGDN_FORPARSING: get full path of id list */
655         HRESULT hr = (*folder->lpVtbl->GetDisplayNameOf)(folder, pidl, SHGDN_FORPARSING, &str);
656
657         if (SUCCEEDED(hr)) {
658                 get_strretW(&str, &pidl->mkid, buffer, len);
659                 free_strret(&str);
660         } else
661                 buffer[0] = '\0';
662
663         return hr;
664 }
665
666
667  /* create an item id list from a file system path */
668
669 static LPITEMIDLIST get_path_pidl(LPTSTR path, HWND hwnd)
670 {
671         LPITEMIDLIST pidl;
672         HRESULT hr;
673         ULONG len;
674
675 #ifdef UNICODE
676         LPWSTR buffer = path;
677 #else
678         WCHAR buffer[MAX_PATH];
679         MultiByteToWideChar(CP_ACP, 0, path, -1, buffer, MAX_PATH);
680 #endif
681
682         hr = (*Globals.iDesktop->lpVtbl->ParseDisplayName)(Globals.iDesktop, hwnd, NULL, buffer, &len, &pidl, NULL);
683         if (FAILED(hr))
684                 return NULL;
685
686         return pidl;
687 }
688
689
690  /* convert an item id list from relative to absolute (=relative to the desktop) format */
691
692 static LPITEMIDLIST get_to_absolute_pidl(Entry* entry, HWND hwnd)
693 {
694         if (entry->up && entry->up->etype==ET_SHELL) {
695                 IShellFolder* folder = entry->up->folder;
696                 WCHAR buffer[MAX_PATH];
697
698                 HRESULT hr = path_from_pidlW(folder, entry->pidl, buffer, MAX_PATH);
699
700                 if (SUCCEEDED(hr)) {
701                         LPITEMIDLIST pidl;
702                         ULONG len;
703
704                         hr = (*Globals.iDesktop->lpVtbl->ParseDisplayName)(Globals.iDesktop, hwnd, NULL, buffer, &len, &pidl, NULL);
705
706                         if (SUCCEEDED(hr))
707                                 return pidl;
708                 }
709         } else if (entry->etype == ET_WINDOWS) {
710                 TCHAR path[MAX_PATH];
711
712                 get_path(entry, path);
713
714                 return get_path_pidl(path, hwnd);
715         } else if (entry->pidl)
716                 return ILClone(entry->pidl);
717
718         return NULL;
719 }
720
721
722 HICON extract_icon(IShellFolder* folder, LPCITEMIDLIST pidl)
723 {
724         IExtractIcon* pExtract;
725
726         if (SUCCEEDED((*folder->lpVtbl->GetUIObjectOf)(folder, 0, 1, (LPCITEMIDLIST*)&pidl, &IID_IExtractIcon, 0, (LPVOID*)&pExtract))) {
727                 TCHAR path[_MAX_PATH];
728                 unsigned flags;
729                 HICON hicon;
730                 int idx;
731
732                 if (SUCCEEDED((*pExtract->lpVtbl->GetIconLocation)(pExtract, GIL_FORSHELL, path, _MAX_PATH, &idx, &flags))) {
733                         if (!(flags & GIL_NOTFILENAME)) {
734                                 if (idx == -1)
735                                         idx = 0;        /* special case for some control panel applications */
736
737                                 if ((int)ExtractIconEx(path, idx, 0, &hicon, 1) > 0)
738                                         flags &= ~GIL_DONTCACHE;
739                         } else {
740                                 HICON hIconLarge = 0;
741
742                                 HRESULT hr = (*pExtract->lpVtbl->Extract)(pExtract, path, idx, &hIconLarge, &hicon, MAKELONG(0/*GetSystemMetrics(SM_CXICON)*/,GetSystemMetrics(SM_CXSMICON)));
743
744                                 if (SUCCEEDED(hr))
745                                         DestroyIcon(hIconLarge);
746                         }
747
748                         return hicon;
749                 }
750         }
751
752         return 0;
753 }
754
755
756 static Entry* find_entry_shell(Entry* dir, LPITEMIDLIST pidl)
757 {
758         Entry* entry;
759
760         for(entry=dir->down; entry; entry=entry->next) {
761                 if (entry->pidl->mkid.cb == pidl->mkid.cb &&
762                         !memcmp(entry->pidl, pidl, entry->pidl->mkid.cb))
763                         return entry;
764         }
765
766         return 0;
767 }
768
769 static Entry* read_tree_shell(Root* root, LPITEMIDLIST pidl, SORT_ORDER sortOrder, HWND hwnd)
770 {
771         Entry* entry = &root->entry;
772         Entry* next;
773         LPITEMIDLIST next_pidl = pidl;
774         IShellFolder* folder;
775         IShellFolder* child = NULL;
776         HRESULT hr;
777
778         HCURSOR old_cursor = SetCursor(LoadCursor(0, IDC_WAIT));
779
780 #ifndef _NO_EXTENSIONS
781         entry->etype = ET_SHELL;
782 #endif
783
784         folder = Globals.iDesktop;
785
786         while(entry) {
787                 entry->pidl = next_pidl;
788                 entry->folder = folder;
789
790                 if (!pidl->mkid.cb)
791                         break;
792
793                  /* copy first element of item idlist */
794                 next_pidl = (*Globals.iMalloc->lpVtbl->Alloc)(Globals.iMalloc, pidl->mkid.cb+sizeof(USHORT));
795                 memcpy(next_pidl, pidl, pidl->mkid.cb);
796                 ((LPITEMIDLIST)((LPBYTE)next_pidl+pidl->mkid.cb))->mkid.cb = 0;
797
798                 hr = (*folder->lpVtbl->BindToObject)(folder, next_pidl, 0, &IID_IShellFolder, (void**)&child);
799                 if (!SUCCEEDED(hr))
800                         break;
801
802                 read_directory(entry, NULL, sortOrder, hwnd);
803
804                 if (entry->down)
805                         entry->expanded = TRUE;
806
807                 next = find_entry_shell(entry, next_pidl);
808                 if (!next)
809                         break;
810
811                 folder = child;
812                 entry = next;
813
814                  /* go to next element */
815                 pidl = (LPITEMIDLIST) ((LPBYTE)pidl+pidl->mkid.cb);
816         }
817
818         SetCursor(old_cursor);
819
820         return entry;
821 }
822
823
824 static void fill_w32fdata_shell(IShellFolder* folder, LPCITEMIDLIST pidl, SFGAOF attribs, WIN32_FIND_DATA* w32fdata)
825 {
826         if (!(attribs & SFGAO_FILESYSTEM) ||
827                   FAILED(SHGetDataFromIDList(folder, pidl, SHGDFIL_FINDDATA, w32fdata, sizeof(WIN32_FIND_DATA)))) {
828                 WIN32_FILE_ATTRIBUTE_DATA fad;
829                 IDataObject* pDataObj;
830
831                 STGMEDIUM medium = {0, {0}, 0};
832                 FORMATETC fmt = {Globals.cfStrFName, 0, DVASPECT_CONTENT, -1, TYMED_HGLOBAL};
833
834                 HRESULT hr = (*folder->lpVtbl->GetUIObjectOf)(folder, 0, 1, &pidl, &IID_IDataObject, 0, (LPVOID*)&pDataObj);
835
836                 if (SUCCEEDED(hr)) {
837                         hr = (*pDataObj->lpVtbl->GetData)(pDataObj, &fmt, &medium);
838
839                         (*pDataObj->lpVtbl->Release)(pDataObj);
840
841                         if (SUCCEEDED(hr)) {
842                                 LPCTSTR path = (LPCTSTR)GlobalLock(medium.UNION_MEMBER(hGlobal));
843                                 UINT sem_org = SetErrorMode(SEM_FAILCRITICALERRORS);
844
845                                 if (GetFileAttributesEx(path, GetFileExInfoStandard, &fad)) {
846                                         w32fdata->dwFileAttributes = fad.dwFileAttributes;
847                                         w32fdata->ftCreationTime = fad.ftCreationTime;
848                                         w32fdata->ftLastAccessTime = fad.ftLastAccessTime;
849                                         w32fdata->ftLastWriteTime = fad.ftLastWriteTime;
850
851                                         if (!(fad.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
852                                                 w32fdata->nFileSizeLow = fad.nFileSizeLow;
853                                                 w32fdata->nFileSizeHigh = fad.nFileSizeHigh;
854                                         }
855                                 }
856
857                                 SetErrorMode(sem_org);
858
859                                 GlobalUnlock(medium.UNION_MEMBER(hGlobal));
860                                 GlobalFree(medium.UNION_MEMBER(hGlobal));
861                         }
862                 }
863         }
864
865         if (attribs & (SFGAO_FOLDER|SFGAO_HASSUBFOLDER))
866                 w32fdata->dwFileAttributes |= FILE_ATTRIBUTE_DIRECTORY;
867
868         if (attribs & SFGAO_READONLY)
869                 w32fdata->dwFileAttributes |= FILE_ATTRIBUTE_READONLY;
870
871         if (attribs & SFGAO_COMPRESSED)
872                 w32fdata->dwFileAttributes |= FILE_ATTRIBUTE_COMPRESSED;
873 }
874
875
876 static void read_directory_shell(Entry* dir, HWND hwnd)
877 {
878         IShellFolder* folder = dir->folder;
879         int level = dir->level + 1;
880         HRESULT hr;
881
882         IShellFolder* child;
883         IEnumIDList* idlist;
884
885         Entry* first_entry = NULL;
886         Entry* last = NULL;
887         Entry* entry;
888
889         if (!folder)
890                 return;
891
892         hr = (*folder->lpVtbl->EnumObjects)(folder, hwnd, SHCONTF_FOLDERS|SHCONTF_NONFOLDERS|SHCONTF_INCLUDEHIDDEN|SHCONTF_SHAREABLE|SHCONTF_STORAGE, &idlist);
893
894         if (SUCCEEDED(hr)) {
895                 for(;;) {
896 #define FETCH_ITEM_COUNT        32
897                         LPITEMIDLIST pidls[FETCH_ITEM_COUNT];
898                         SFGAOF attribs;
899                         ULONG cnt = 0;
900                         ULONG n;
901
902                         memset(pidls, 0, sizeof(pidls));
903
904                         hr = (*idlist->lpVtbl->Next)(idlist, FETCH_ITEM_COUNT, pidls, &cnt);
905                         if (!SUCCEEDED(hr))
906                                 break;
907
908                         if (hr == S_FALSE)
909                                 break;
910
911                         for(n=0; n<cnt; ++n) {
912                                 entry = alloc_entry();
913
914                                 if (!first_entry)
915                                         first_entry = entry;
916
917                                 if (last)
918                                         last->next = entry;
919
920                                 memset(&entry->data, 0, sizeof(WIN32_FIND_DATA));
921                                 entry->bhfi_valid = FALSE;
922
923                                 attribs = ~SFGAO_FILESYSTEM;    /*SFGAO_HASSUBFOLDER|SFGAO_FOLDER; SFGAO_FILESYSTEM sorgt dafür, daß "My Documents" anstatt von "Martin's Documents" angezeigt wird */
924
925                                 hr = (*folder->lpVtbl->GetAttributesOf)(folder, 1, (LPCITEMIDLIST*)&pidls[n], &attribs);
926
927                                 if (SUCCEEDED(hr)) {
928                                         if (attribs != (SFGAOF)~SFGAO_FILESYSTEM) {
929                                                 fill_w32fdata_shell(folder, pidls[n], attribs, &entry->data);
930
931                                                 entry->bhfi_valid = TRUE;
932                                         } else
933                                                 attribs = 0;
934                                 } else
935                                         attribs = 0;
936
937                                 entry->pidl = pidls[n];
938
939                                 if (entry->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
940                                         hr = (*folder->lpVtbl->BindToObject)(folder, pidls[n], 0, &IID_IShellFolder, (void**)&child);
941
942                                         if (SUCCEEDED(hr))
943                                                 entry->folder = child;
944                                         else
945                                                 entry->folder = NULL;
946                                 }
947                                 else
948                                         entry->folder = NULL;
949
950                                 if (!entry->data.cFileName[0])
951                                         /*hr = */name_from_pidl(folder, pidls[n], entry->data.cFileName, MAX_PATH, /*SHGDN_INFOLDER*/0x2000/*0x2000=SHGDN_INCLUDE_NONFILESYS*/);
952
953                                  /* get display icons for files and virtual objects */
954                                 if (!(entry->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
955                                         !(attribs & SFGAO_FILESYSTEM)) {
956                                         entry->hicon = extract_icon(folder, pidls[n]);
957
958                                         if (!entry->hicon)
959                                                 entry->hicon = (HICON)-1;       /* don't try again later */
960                                 }
961
962                                 entry->down = NULL;
963                                 entry->up = dir;
964                                 entry->expanded = FALSE;
965                                 entry->scanned = FALSE;
966                                 entry->level = level;
967
968 #ifndef _NO_EXTENSIONS
969                                 entry->etype = ET_SHELL;
970                                 entry->bhfi_valid = FALSE;
971 #endif
972
973                                 last = entry;
974                         }
975                 }
976
977                 (*idlist->lpVtbl->Release)(idlist);
978         }
979
980         if (last)
981                 last->next = NULL;
982
983         dir->down = first_entry;
984         dir->scanned = TRUE;
985 }
986
987 #endif /* _SHELL_FOLDERS */
988
989
990 /* directories first... */
991 static int compareType(const WIN32_FIND_DATA* fd1, const WIN32_FIND_DATA* fd2)
992 {
993         int dir1 = fd1->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
994         int dir2 = fd2->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
995
996         return dir2==dir1? 0: dir2<dir1? -1: 1;
997 }
998
999
1000 static int compareName(const void* arg1, const void* arg2)
1001 {
1002         const WIN32_FIND_DATA* fd1 = &(*(Entry**)arg1)->data;
1003         const WIN32_FIND_DATA* fd2 = &(*(Entry**)arg2)->data;
1004
1005         int cmp = compareType(fd1, fd2);
1006         if (cmp)
1007                 return cmp;
1008
1009         return lstrcmpi(fd1->cFileName, fd2->cFileName);
1010 }
1011
1012 static int compareExt(const void* arg1, const void* arg2)
1013 {
1014         const WIN32_FIND_DATA* fd1 = &(*(Entry**)arg1)->data;
1015         const WIN32_FIND_DATA* fd2 = &(*(Entry**)arg2)->data;
1016         const TCHAR *name1, *name2, *ext1, *ext2;
1017
1018         int cmp = compareType(fd1, fd2);
1019         if (cmp)
1020                 return cmp;
1021
1022         name1 = fd1->cFileName;
1023         name2 = fd2->cFileName;
1024
1025         ext1 = _tcsrchr(name1, TEXT('.'));
1026         ext2 = _tcsrchr(name2, TEXT('.'));
1027
1028         if (ext1)
1029                 ext1++;
1030         else
1031                 ext1 = TEXT("");
1032
1033         if (ext2)
1034                 ext2++;
1035         else
1036                 ext2 = TEXT("");
1037
1038         cmp = lstrcmpi(ext1, ext2);
1039         if (cmp)
1040                 return cmp;
1041
1042         return lstrcmpi(name1, name2);
1043 }
1044
1045 static int compareSize(const void* arg1, const void* arg2)
1046 {
1047         WIN32_FIND_DATA* fd1 = &(*(Entry**)arg1)->data;
1048         WIN32_FIND_DATA* fd2 = &(*(Entry**)arg2)->data;
1049
1050         int cmp = compareType(fd1, fd2);
1051         if (cmp)
1052                 return cmp;
1053
1054         cmp = fd2->nFileSizeHigh - fd1->nFileSizeHigh;
1055
1056         if (cmp < 0)
1057                 return -1;
1058         else if (cmp > 0)
1059                 return 1;
1060
1061         cmp = fd2->nFileSizeLow - fd1->nFileSizeLow;
1062
1063         return cmp<0? -1: cmp>0? 1: 0;
1064 }
1065
1066 static int compareDate(const void* arg1, const void* arg2)
1067 {
1068         WIN32_FIND_DATA* fd1 = &(*(Entry**)arg1)->data;
1069         WIN32_FIND_DATA* fd2 = &(*(Entry**)arg2)->data;
1070
1071         int cmp = compareType(fd1, fd2);
1072         if (cmp)
1073                 return cmp;
1074
1075         return CompareFileTime(&fd2->ftLastWriteTime, &fd1->ftLastWriteTime);
1076 }
1077
1078
1079 static int (*sortFunctions[])(const void* arg1, const void* arg2) = {
1080         compareName,    /* SORT_NAME */
1081         compareExt,             /* SORT_EXT */
1082         compareSize,    /* SORT_SIZE */
1083         compareDate             /* SORT_DATE */
1084 };
1085
1086
1087 static void SortDirectory(Entry* dir, SORT_ORDER sortOrder)
1088 {
1089         Entry* entry = dir->down;
1090         Entry** array, **p;
1091         int len;
1092
1093         len = 0;
1094         for(entry=dir->down; entry; entry=entry->next)
1095                 len++;
1096
1097         if (len) {
1098                 array = HeapAlloc(GetProcessHeap(), 0, len*sizeof(Entry*));
1099
1100                 p = array;
1101                 for(entry=dir->down; entry; entry=entry->next)
1102                         *p++ = entry;
1103
1104                 /* call qsort with the appropriate compare function */
1105                 qsort(array, len, sizeof(array[0]), sortFunctions[sortOrder]);
1106
1107                 dir->down = array[0];
1108
1109                 for(p=array; --len; p++)
1110                         p[0]->next = p[1];
1111
1112                 (*p)->next = 0;
1113                 HeapFree( GetProcessHeap(), 0, array );
1114         }
1115 }
1116
1117
1118 static void read_directory(Entry* dir, LPCTSTR path, SORT_ORDER sortOrder, HWND hwnd)
1119 {
1120         TCHAR buffer[MAX_PATH];
1121         Entry* entry;
1122         LPCTSTR s;
1123         PTSTR d;
1124
1125 #ifdef _SHELL_FOLDERS
1126         if (dir->etype == ET_SHELL)
1127         {
1128                 read_directory_shell(dir, hwnd);
1129
1130                 if (Globals.prescan_node) {
1131                         s = path;
1132                         d = buffer;
1133
1134                         while(*s)
1135                                 *d++ = *s++;
1136
1137                         *d++ = TEXT('\\');
1138
1139                         for(entry=dir->down; entry; entry=entry->next)
1140                                 if (entry->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
1141                                         read_directory_shell(entry, hwnd);
1142                                         SortDirectory(entry, sortOrder);
1143                                 }
1144                 }
1145         }
1146         else
1147 #endif
1148 #if !defined(_NO_EXTENSIONS) && defined(__WINE__)
1149         if (dir->etype == ET_UNIX)
1150         {
1151                 read_directory_unix(dir, path);
1152
1153                 if (Globals.prescan_node) {
1154                         s = path;
1155                         d = buffer;
1156
1157                         while(*s)
1158                                 *d++ = *s++;
1159
1160                         *d++ = TEXT('/');
1161
1162                         for(entry=dir->down; entry; entry=entry->next)
1163                                 if (entry->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
1164                                         lstrcpy(d, entry->data.cFileName);
1165                                         read_directory_unix(entry, buffer);
1166                                         SortDirectory(entry, sortOrder);
1167                                 }
1168                 }
1169         }
1170         else
1171 #endif
1172         {
1173                 read_directory_win(dir, path);
1174
1175                 if (Globals.prescan_node) {
1176                         s = path;
1177                         d = buffer;
1178
1179                         while(*s)
1180                                 *d++ = *s++;
1181
1182                         *d++ = TEXT('\\');
1183
1184                         for(entry=dir->down; entry; entry=entry->next)
1185                                 if (entry->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
1186                                         lstrcpy(d, entry->data.cFileName);
1187                                         read_directory_win(entry, buffer);
1188                                         SortDirectory(entry, sortOrder);
1189                                 }
1190                 }
1191         }
1192
1193         SortDirectory(dir, sortOrder);
1194 }
1195
1196
1197 static ChildWnd* alloc_child_window(LPCTSTR path, LPITEMIDLIST pidl, HWND hwnd)
1198 {
1199         TCHAR drv[_MAX_DRIVE+1], dir[_MAX_DIR], name[_MAX_FNAME], ext[_MAX_EXT];
1200         ChildWnd* child = (ChildWnd*) malloc(sizeof(ChildWnd));
1201         Root* root = &child->root;
1202         Entry* entry;
1203
1204         memset(child, 0, sizeof(ChildWnd));
1205
1206         child->left.treePane = TRUE;
1207         child->left.visible_cols = 0;
1208
1209         child->right.treePane = FALSE;
1210 #ifndef _NO_EXTENSIONS
1211         child->right.visible_cols = COL_SIZE|COL_DATE|COL_TIME|COL_ATTRIBUTES|COL_INDEX|COL_LINKS;
1212 #else
1213         child->right.visible_cols = COL_SIZE|COL_DATE|COL_TIME|COL_ATTRIBUTES;
1214 #endif
1215
1216         child->pos.length = sizeof(WINDOWPLACEMENT);
1217         child->pos.flags = 0;
1218         child->pos.showCmd = SW_SHOWNORMAL;
1219         child->pos.rcNormalPosition.left = CW_USEDEFAULT;
1220         child->pos.rcNormalPosition.top = CW_USEDEFAULT;
1221         child->pos.rcNormalPosition.right = CW_USEDEFAULT;
1222         child->pos.rcNormalPosition.bottom = CW_USEDEFAULT;
1223
1224         child->focus_pane = 0;
1225         child->split_pos = DEFAULT_SPLIT_POS;
1226         child->sortOrder = SORT_NAME;
1227         child->header_wdths_ok = FALSE;
1228
1229         if (path)
1230         {
1231                 lstrcpy(child->path, path);
1232
1233                 _tsplitpath(path, drv, dir, name, ext);
1234         }
1235
1236         root->entry.level = 0;
1237
1238 #ifdef _SHELL_FOLDERS
1239         if (pidl)
1240         {
1241                 root->drive_type = DRIVE_UNKNOWN;
1242                 lstrcpy(drv, TEXT("\\"));
1243                 lstrcpy(root->volname, TEXT("Desktop"));
1244                 root->fs_flags = 0;
1245                 lstrcpy(root->fs, TEXT("Shell"));
1246
1247                 entry = read_tree_shell(root, pidl, child->sortOrder, hwnd);
1248         }
1249         else
1250 #endif
1251 #if !defined(_NO_EXTENSIONS) && defined(__WINE__)
1252         if (*path == '/')
1253         {
1254                 root->drive_type = GetDriveType(path);
1255
1256                 lstrcat(drv, TEXT("/"));
1257                 lstrcpy(root->volname, TEXT("root fs"));
1258                 root->fs_flags = 0;
1259                 lstrcpy(root->fs, TEXT("unixfs"));
1260
1261                 lstrcpy(root->path, TEXT("/"));
1262                 entry = read_tree_unix(root, path, child->sortOrder, hwnd);
1263         }
1264         else
1265 #endif
1266         {
1267                 root->drive_type = GetDriveType(path);
1268
1269                 lstrcat(drv, TEXT("\\"));
1270                 GetVolumeInformation(drv, root->volname, _MAX_FNAME, 0, 0, &root->fs_flags, root->fs, _MAX_DIR);
1271
1272                 lstrcpy(root->path, drv);
1273                 entry = read_tree_win(root, path, child->sortOrder, hwnd);
1274         }
1275
1276 #ifdef _SHELL_FOLDERS
1277         if (root->entry.etype == ET_SHELL)
1278                 lstrcpy(root->entry.data.cFileName, TEXT("Desktop"));
1279         else
1280 #endif
1281                 wsprintf(root->entry.data.cFileName, TEXT("%s - %s"), drv, root->fs);
1282
1283         root->entry.data.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1284
1285         child->left.root = &root->entry;
1286         child->right.root = NULL;
1287
1288         set_curdir(child, entry, hwnd);
1289
1290         return child;
1291 }
1292
1293
1294 /* free all memory associated with a child window */
1295 static void free_child_window(ChildWnd* child)
1296 {
1297         free_entries(&child->root.entry);
1298         free(child);
1299 }
1300
1301
1302 /* get full path of specified directory entry */
1303 static void get_path(Entry* dir, PTSTR path)
1304 {
1305         Entry* entry;
1306         int len = 0;
1307         int level = 0;
1308
1309 #ifdef _SHELL_FOLDERS
1310         if (dir->etype == ET_SHELL)
1311         {
1312                 SFGAOF attribs;
1313                 HRESULT hr = S_OK;
1314
1315                 path[0] = TEXT('\0');
1316
1317                 attribs = 0;
1318
1319                 if (dir->folder)
1320                         hr = (*dir->folder->lpVtbl->GetAttributesOf)(dir->folder, 1, (LPCITEMIDLIST*)&dir->pidl, &attribs);
1321
1322                 if (SUCCEEDED(hr) && (attribs&SFGAO_FILESYSTEM)) {
1323                         IShellFolder* parent = dir->up? dir->up->folder: Globals.iDesktop;
1324
1325                         hr = path_from_pidl(parent, dir->pidl, path, MAX_PATH);
1326                 }
1327         }
1328         else
1329 #endif
1330         {
1331                 for(entry=dir; entry; level++) {
1332                         LPCTSTR name;
1333                         int l;
1334
1335                         {
1336                                 LPCTSTR s;
1337                                 name = entry->data.cFileName;
1338                                 s = name;
1339
1340                                 for(l=0; *s && *s!=TEXT('/') && *s!=TEXT('\\'); s++)
1341                                         l++;
1342                         }
1343
1344                         if (entry->up) {
1345                                 if (l > 0) {
1346                                         memmove(path+l+1, path, len*sizeof(TCHAR));
1347                                         memcpy(path+1, name, l*sizeof(TCHAR));
1348                                         len += l+1;
1349
1350 #ifndef _NO_EXTENSIONS
1351                                         if (entry->etype == ET_UNIX)
1352                                                 path[0] = TEXT('/');
1353                                         else
1354 #endif
1355                                         path[0] = TEXT('\\');
1356                                 }
1357
1358                                 entry = entry->up;
1359                         } else {
1360                                 memmove(path+l, path, len*sizeof(TCHAR));
1361                                 memcpy(path, name, l*sizeof(TCHAR));
1362                                 len += l;
1363                                 break;
1364                         }
1365                 }
1366
1367                 if (!level) {
1368 #ifndef _NO_EXTENSIONS
1369                         if (entry->etype == ET_UNIX)
1370                                 path[len++] = TEXT('/');
1371                         else
1372 #endif
1373                                 path[len++] = TEXT('\\');
1374                 }
1375
1376                 path[len] = TEXT('\0');
1377         }
1378 }
1379
1380
1381 static void resize_frame_rect(HWND hwnd, PRECT prect)
1382 {
1383         int new_top;
1384         RECT rt;
1385
1386         if (IsWindowVisible(Globals.htoolbar)) {
1387                 SendMessage(Globals.htoolbar, WM_SIZE, 0, 0);
1388                 GetClientRect(Globals.htoolbar, &rt);
1389                 prect->top = rt.bottom+3;
1390                 prect->bottom -= rt.bottom+3;
1391         }
1392
1393         if (IsWindowVisible(Globals.hdrivebar)) {
1394                 SendMessage(Globals.hdrivebar, WM_SIZE, 0, 0);
1395                 GetClientRect(Globals.hdrivebar, &rt);
1396                 new_top = --prect->top + rt.bottom+3;
1397                 MoveWindow(Globals.hdrivebar, 0, prect->top, rt.right, new_top, TRUE);
1398                 prect->top = new_top;
1399                 prect->bottom -= rt.bottom+2;
1400         }
1401
1402         if (IsWindowVisible(Globals.hstatusbar)) {
1403                 int parts[] = {300, 500};
1404
1405                 SendMessage(Globals.hstatusbar, WM_SIZE, 0, 0);
1406                 SendMessage(Globals.hstatusbar, SB_SETPARTS, 2, (LPARAM)&parts);
1407                 GetClientRect(Globals.hstatusbar, &rt);
1408                 prect->bottom -= rt.bottom;
1409         }
1410
1411         MoveWindow(Globals.hmdiclient, prect->left-1,prect->top-1,prect->right+2,prect->bottom+1, TRUE);
1412 }
1413
1414 static void resize_frame(HWND hwnd, int cx, int cy)
1415 {
1416         RECT rect;
1417
1418         rect.left   = 0;
1419         rect.top    = 0;
1420         rect.right  = cx;
1421         rect.bottom = cy;
1422
1423         resize_frame_rect(hwnd, &rect);
1424 }
1425
1426 static void resize_frame_client(HWND hwnd)
1427 {
1428         RECT rect;
1429
1430         GetClientRect(hwnd, &rect);
1431
1432         resize_frame_rect(hwnd, &rect);
1433 }
1434
1435
1436 static HHOOK hcbthook;
1437 static ChildWnd* newchild = NULL;
1438
1439 LRESULT CALLBACK CBTProc(int code, WPARAM wparam, LPARAM lparam)
1440 {
1441         if (code==HCBT_CREATEWND && newchild) {
1442                 ChildWnd* child = newchild;
1443                 newchild = NULL;
1444
1445                 child->hwnd = (HWND) wparam;
1446                 SetWindowLong(child->hwnd, GWL_USERDATA, (LPARAM)child);
1447         }
1448
1449         return CallNextHookEx(hcbthook, code, wparam, lparam);
1450 }
1451
1452 static HWND create_child_window(ChildWnd* child)
1453 {
1454         MDICREATESTRUCT mcs;
1455         int idx;
1456
1457         mcs.szClass = WINEFILETREE;
1458         mcs.szTitle = (LPTSTR)child->path;
1459         mcs.hOwner  = Globals.hInstance;
1460         mcs.x       = child->pos.rcNormalPosition.left;
1461         mcs.y       = child->pos.rcNormalPosition.top;
1462         mcs.cx      = child->pos.rcNormalPosition.right-child->pos.rcNormalPosition.left;
1463         mcs.cy      = child->pos.rcNormalPosition.bottom-child->pos.rcNormalPosition.top;
1464         mcs.style   = 0;
1465         mcs.lParam  = 0;
1466
1467         hcbthook = SetWindowsHookEx(WH_CBT, CBTProc, 0, GetCurrentThreadId());
1468
1469         newchild = child;
1470         child->hwnd = (HWND) SendMessage(Globals.hmdiclient, WM_MDICREATE, 0, (LPARAM)&mcs);
1471         if (!child->hwnd) {
1472                 UnhookWindowsHookEx(hcbthook);
1473                 return 0;
1474         }
1475
1476         UnhookWindowsHookEx(hcbthook);
1477
1478         idx = ListBox_FindItemData(child->left.hwnd, ListBox_GetCurSel(child->left.hwnd), child->left.cur);
1479         ListBox_SetCurSel(child->left.hwnd, idx);
1480
1481         return child->hwnd;
1482 }
1483
1484
1485 struct ExecuteDialog {
1486         TCHAR   cmd[MAX_PATH];
1487         int             cmdshow;
1488 };
1489
1490
1491 static BOOL CALLBACK ExecuteDialogWndProg(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam)
1492 {
1493         static struct ExecuteDialog* dlg;
1494
1495         switch(nmsg) {
1496                 case WM_INITDIALOG:
1497                         dlg = (struct ExecuteDialog*) lparam;
1498                         return 1;
1499
1500                 case WM_COMMAND: {
1501                         int id = (int)wparam;
1502
1503                         if (id == IDOK) {
1504                                 GetWindowText(GetDlgItem(hwnd, 201), dlg->cmd, MAX_PATH);
1505                                 dlg->cmdshow = Button_GetState(GetDlgItem(hwnd,214))&BST_CHECKED?
1506                                                                                                 SW_SHOWMINIMIZED: SW_SHOWNORMAL;
1507                                 EndDialog(hwnd, id);
1508                         } else if (id == IDCANCEL)
1509                                 EndDialog(hwnd, id);
1510
1511                         return 1;}
1512         }
1513
1514         return 0;
1515 }
1516
1517
1518 #ifndef _NO_EXTENSIONS
1519
1520 static struct FullScreenParameters {
1521         BOOL    mode;
1522         RECT    orgPos;
1523         BOOL    wasZoomed;
1524 } g_fullscreen = {
1525     FALSE,      /* mode */
1526         {0, 0, 0, 0},
1527         FALSE
1528 };
1529
1530 void frame_get_clientspace(HWND hwnd, PRECT prect)
1531 {
1532         RECT rt;
1533
1534         if (!IsIconic(hwnd))
1535                 GetClientRect(hwnd, prect);
1536         else {
1537                 WINDOWPLACEMENT wp;
1538
1539                 GetWindowPlacement(hwnd, &wp);
1540
1541                 prect->left = prect->top = 0;
1542                 prect->right = wp.rcNormalPosition.right-wp.rcNormalPosition.left-
1543                                                 2*(GetSystemMetrics(SM_CXSIZEFRAME)+GetSystemMetrics(SM_CXEDGE));
1544                 prect->bottom = wp.rcNormalPosition.bottom-wp.rcNormalPosition.top-
1545                                                 2*(GetSystemMetrics(SM_CYSIZEFRAME)+GetSystemMetrics(SM_CYEDGE))-
1546                                                 GetSystemMetrics(SM_CYCAPTION)-GetSystemMetrics(SM_CYMENUSIZE);
1547         }
1548
1549         if (IsWindowVisible(Globals.htoolbar)) {
1550                 GetClientRect(Globals.htoolbar, &rt);
1551                 prect->top += rt.bottom+2;
1552         }
1553
1554         if (IsWindowVisible(Globals.hdrivebar)) {
1555                 GetClientRect(Globals.hdrivebar, &rt);
1556                 prect->top += rt.bottom+2;
1557         }
1558
1559         if (IsWindowVisible(Globals.hstatusbar)) {
1560                 GetClientRect(Globals.hstatusbar, &rt);
1561                 prect->bottom -= rt.bottom;
1562         }
1563 }
1564
1565 static BOOL toggle_fullscreen(HWND hwnd)
1566 {
1567         RECT rt;
1568
1569         if ((g_fullscreen.mode=!g_fullscreen.mode)) {
1570                 GetWindowRect(hwnd, &g_fullscreen.orgPos);
1571                 g_fullscreen.wasZoomed = IsZoomed(hwnd);
1572
1573                 Frame_CalcFrameClient(hwnd, &rt);
1574                 ClientToScreen(hwnd, (LPPOINT)&rt.left);
1575                 ClientToScreen(hwnd, (LPPOINT)&rt.right);
1576
1577                 rt.left = g_fullscreen.orgPos.left-rt.left;
1578                 rt.top = g_fullscreen.orgPos.top-rt.top;
1579                 rt.right = GetSystemMetrics(SM_CXSCREEN)+g_fullscreen.orgPos.right-rt.right;
1580                 rt.bottom = GetSystemMetrics(SM_CYSCREEN)+g_fullscreen.orgPos.bottom-rt.bottom;
1581
1582                 MoveWindow(hwnd, rt.left, rt.top, rt.right-rt.left, rt.bottom-rt.top, TRUE);
1583         } else {
1584                 MoveWindow(hwnd, g_fullscreen.orgPos.left, g_fullscreen.orgPos.top,
1585                                                         g_fullscreen.orgPos.right-g_fullscreen.orgPos.left,
1586                                                         g_fullscreen.orgPos.bottom-g_fullscreen.orgPos.top, TRUE);
1587
1588                 if (g_fullscreen.wasZoomed)
1589                         ShowWindow(hwnd, WS_MAXIMIZE);
1590         }
1591
1592         return g_fullscreen.mode;
1593 }
1594
1595 static void fullscreen_move(HWND hwnd)
1596 {
1597         RECT rt, pos;
1598         GetWindowRect(hwnd, &pos);
1599
1600         Frame_CalcFrameClient(hwnd, &rt);
1601         ClientToScreen(hwnd, (LPPOINT)&rt.left);
1602         ClientToScreen(hwnd, (LPPOINT)&rt.right);
1603
1604         rt.left = pos.left-rt.left;
1605         rt.top = pos.top-rt.top;
1606         rt.right = GetSystemMetrics(SM_CXSCREEN)+pos.right-rt.right;
1607         rt.bottom = GetSystemMetrics(SM_CYSCREEN)+pos.bottom-rt.bottom;
1608
1609         MoveWindow(hwnd, rt.left, rt.top, rt.right-rt.left, rt.bottom-rt.top, TRUE);
1610 }
1611
1612 #endif
1613
1614
1615 static void toggle_child(HWND hwnd, UINT cmd, HWND hchild)
1616 {
1617         BOOL vis = IsWindowVisible(hchild);
1618
1619         CheckMenuItem(Globals.hMenuOptions, cmd, vis?MF_BYCOMMAND:MF_BYCOMMAND|MF_CHECKED);
1620
1621         ShowWindow(hchild, vis?SW_HIDE:SW_SHOW);
1622
1623 #ifndef _NO_EXTENSIONS
1624         if (g_fullscreen.mode)
1625                 fullscreen_move(hwnd);
1626 #endif
1627
1628         resize_frame_client(hwnd);
1629 }
1630
1631 BOOL activate_drive_window(LPCTSTR path)
1632 {
1633         TCHAR drv1[_MAX_DRIVE], drv2[_MAX_DRIVE];
1634         HWND child_wnd;
1635
1636         _tsplitpath(path, drv1, 0, 0, 0);
1637
1638         /* search for a already open window for the same drive */
1639         for(child_wnd=GetNextWindow(Globals.hmdiclient,GW_CHILD); child_wnd; child_wnd=GetNextWindow(child_wnd, GW_HWNDNEXT)) {
1640                 ChildWnd* child = (ChildWnd*) GetWindowLong(child_wnd, GWL_USERDATA);
1641
1642                 if (child) {
1643                         _tsplitpath(child->root.path, drv2, 0, 0, 0);
1644
1645                         if (!lstrcmpi(drv2, drv1)) {
1646                                 SendMessage(Globals.hmdiclient, WM_MDIACTIVATE, (WPARAM)child_wnd, 0);
1647
1648                                 if (IsMinimized(child_wnd))
1649                                         ShowWindow(child_wnd, SW_SHOWNORMAL);
1650
1651                                 return TRUE;
1652                         }
1653                 }
1654         }
1655
1656         return FALSE;
1657 }
1658
1659 BOOL activate_fs_window(LPCTSTR filesys)
1660 {
1661         HWND child_wnd;
1662
1663         /* search for a already open window of the given file system name */
1664         for(child_wnd=GetNextWindow(Globals.hmdiclient,GW_CHILD); child_wnd; child_wnd=GetNextWindow(child_wnd, GW_HWNDNEXT)) {
1665                 ChildWnd* child = (ChildWnd*) GetWindowLong(child_wnd, GWL_USERDATA);
1666
1667                 if (child) {
1668                         if (!lstrcmpi(child->root.fs, filesys)) {
1669                                 SendMessage(Globals.hmdiclient, WM_MDIACTIVATE, (WPARAM)child_wnd, 0);
1670
1671                                 if (IsMinimized(child_wnd))
1672                                         ShowWindow(child_wnd, SW_SHOWNORMAL);
1673
1674                                 return TRUE;
1675                         }
1676                 }
1677         }
1678
1679         return FALSE;
1680 }
1681
1682 LRESULT CALLBACK FrameWndProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam)
1683 {
1684         switch(nmsg) {
1685                 case WM_CLOSE:
1686                         DestroyWindow(hwnd);
1687
1688                          /* clear handle variables */
1689                         Globals.hMenuFrame = 0;
1690                         Globals.hMenuView = 0;
1691                         Globals.hMenuOptions = 0;
1692                         Globals.hMainWnd = 0;
1693                         Globals.hmdiclient = 0;
1694                         Globals.hdrivebar = 0;
1695                         break;
1696
1697                 case WM_DESTROY:
1698                          /* don't exit desktop when closing file manager window */
1699                         if (!Globals.hwndParent)
1700                                 PostQuitMessage(0);
1701                         break;
1702
1703                 case WM_COMMAND: {
1704                         UINT cmd = LOWORD(wparam);
1705                         HWND hwndClient = (HWND) SendMessage(Globals.hmdiclient, WM_MDIGETACTIVE, 0, 0);
1706
1707                         if (SendMessage(hwndClient, WM_DISPATCH_COMMAND, wparam, lparam))
1708                                 break;
1709
1710                         if (cmd>=ID_DRIVE_FIRST && cmd<=ID_DRIVE_FIRST+0xFF) {
1711                                 TCHAR drv[_MAX_DRIVE], path[MAX_PATH];
1712                                 ChildWnd* child;
1713                                 LPCTSTR root = Globals.drives;
1714                                 int i;
1715
1716                                 for(i=cmd-ID_DRIVE_FIRST; i--; root++)
1717                                         while(*root)
1718                                                 root++;
1719
1720                                 if (activate_drive_window(root))
1721                                         return 0;
1722
1723                                 _tsplitpath(root, drv, 0, 0, 0);
1724
1725                                 if (!SetCurrentDirectory(drv)) {
1726                                         display_error(hwnd, GetLastError());
1727                                         return 0;
1728                                 }
1729
1730                                 GetCurrentDirectory(MAX_PATH, path); /*TODO: store last directory per drive */
1731                                 child = alloc_child_window(path, NULL, hwnd);
1732
1733                                 if (!create_child_window(child))
1734                                         free(child);
1735                         } else switch(cmd) {
1736                                 case ID_FILE_EXIT:
1737                                         SendMessage(hwnd, WM_CLOSE, 0, 0);
1738                                         break;
1739
1740                                 case ID_WINDOW_NEW: {
1741                                         TCHAR path[MAX_PATH];
1742                                         ChildWnd* child;
1743
1744                                         GetCurrentDirectory(MAX_PATH, path);
1745                                         child = alloc_child_window(path, NULL, hwnd);
1746
1747                                         if (!create_child_window(child))
1748                                                 free(child);
1749                                         break;}
1750
1751                                 case ID_WINDOW_CASCADE:
1752                                         SendMessage(Globals.hmdiclient, WM_MDICASCADE, 0, 0);
1753                                         break;
1754
1755                                 case ID_WINDOW_TILE_HORZ:
1756                                         SendMessage(Globals.hmdiclient, WM_MDITILE, MDITILE_HORIZONTAL, 0);
1757                                         break;
1758
1759                                 case ID_WINDOW_TILE_VERT:
1760                                         SendMessage(Globals.hmdiclient, WM_MDITILE, MDITILE_VERTICAL, 0);
1761                                         break;
1762
1763                                 case ID_WINDOW_ARRANGE:
1764                                         SendMessage(Globals.hmdiclient, WM_MDIICONARRANGE, 0, 0);
1765                                         break;
1766
1767                                 case ID_VIEW_TOOL_BAR:
1768                                         toggle_child(hwnd, cmd, Globals.htoolbar);
1769                                         break;
1770
1771                                 case ID_VIEW_DRIVE_BAR:
1772                                         toggle_child(hwnd, cmd, Globals.hdrivebar);
1773                                         break;
1774
1775                                 case ID_VIEW_STATUSBAR:
1776                                         toggle_child(hwnd, cmd, Globals.hstatusbar);
1777                                         break;
1778
1779                                 case ID_EXECUTE: {
1780                                         struct ExecuteDialog dlg;
1781
1782                                         memset(&dlg, 0, sizeof(struct ExecuteDialog));
1783
1784                                         if (DialogBoxParam(Globals.hInstance, MAKEINTRESOURCE(IDD_EXECUTE), hwnd, ExecuteDialogWndProg, (LPARAM)&dlg) == IDOK) {
1785                                                 HINSTANCE hinst = ShellExecute(hwnd, NULL/*operation*/, dlg.cmd/*file*/, NULL/*parameters*/, NULL/*dir*/, dlg.cmdshow);
1786
1787                                                 if ((int)hinst <= 32)
1788                                                         display_error(hwnd, GetLastError());
1789                                         }
1790                                         break;}
1791
1792                                 case ID_HELP:
1793                                         WinHelp(hwnd, TEXT("winfile"), HELP_INDEX, 0);
1794                                         break;
1795
1796 #ifndef _NO_EXTENSIONS
1797                                 case ID_VIEW_FULLSCREEN:
1798                                         CheckMenuItem(Globals.hMenuOptions, cmd, toggle_fullscreen(hwnd)?MF_CHECKED:0);
1799                                         break;
1800
1801 #ifdef __WINE__
1802                                 case ID_DRIVE_UNIX_FS: {
1803                                         TCHAR path[MAX_PATH];
1804                                         ChildWnd* child;
1805
1806                                         if (activate_fs_window(TEXT("unixfs")))
1807                                                 break;
1808
1809                                         getcwd(path, MAX_PATH);
1810                                         child = alloc_child_window(path, NULL, hwnd);
1811
1812                                         if (!create_child_window(child))
1813                                                 free(child);
1814                                         break;}
1815 #endif
1816 #ifdef _SHELL_FOLDERS
1817                                 case ID_DRIVE_SHELL_NS: {
1818                                         TCHAR path[MAX_PATH];
1819                                         ChildWnd* child;
1820
1821                                         if (activate_fs_window(TEXT("Shell")))
1822                                                 break;
1823
1824                                         GetCurrentDirectory(MAX_PATH, path);
1825                                         child = alloc_child_window(path, get_path_pidl(path,hwnd), hwnd);
1826
1827                                         if (!create_child_window(child))
1828                                                 free(child);
1829                                         break;}
1830 #endif
1831 #endif
1832
1833                                 /*TODO: There are even more menu items! */
1834
1835 #ifndef _NO_EXTENSIONS
1836 #ifdef __WINE__
1837                                 case ID_LICENSE:
1838                                         WineLicense(Globals.hMainWnd);
1839                                         break;
1840
1841                                 case ID_NO_WARRANTY:
1842                                         WineWarranty(Globals.hMainWnd);
1843                                         break;
1844 #endif
1845
1846                                 case ID_ABOUT_WINE:
1847                                         ShellAbout(hwnd, TEXT("WINE"), TEXT("Winefile"), 0);
1848                                         break;
1849
1850                                 case ID_ABOUT:  /*ID_ABOUT_WINE: */
1851                                         ShellAbout(hwnd, TEXT("Winefile"), NULL, 0);
1852                                         break;
1853 #endif  /* _NO_EXTENSIONS */
1854
1855                                 default:
1856                                         /*TODO: if (wParam >= PM_FIRST_LANGUAGE && wParam <= PM_LAST_LANGUAGE)
1857                                                 STRING_SelectLanguageByNumber(wParam - PM_FIRST_LANGUAGE);
1858                                         else */if ((cmd<IDW_FIRST_CHILD || cmd>=IDW_FIRST_CHILD+0x100) &&
1859                                                 (cmd<SC_SIZE || cmd>SC_RESTORE))
1860                                                 MessageBox(hwnd, TEXT("Not yet implemented"), TEXT("Winefile"), MB_OK);
1861
1862                                         return DefFrameProc(hwnd, Globals.hmdiclient, nmsg, wparam, lparam);
1863                         }
1864                         break;}
1865
1866                 case WM_SIZE:
1867                         resize_frame(hwnd, LOWORD(lparam), HIWORD(lparam));
1868                         break;  /* do not pass message to DefFrameProc */
1869
1870 #ifndef _NO_EXTENSIONS
1871                 case WM_GETMINMAXINFO: {
1872                         LPMINMAXINFO lpmmi = (LPMINMAXINFO)lparam;
1873
1874                         lpmmi->ptMaxTrackSize.x <<= 1;/*2*GetSystemMetrics(SM_CXSCREEN) / SM_CXVIRTUALSCREEN */
1875                         lpmmi->ptMaxTrackSize.y <<= 1;/*2*GetSystemMetrics(SM_CYSCREEN) / SM_CYVIRTUALSCREEN */
1876                         break;}
1877
1878                 case FRM_CALC_CLIENT:
1879                         frame_get_clientspace(hwnd, (PRECT)lparam);
1880                         return TRUE;
1881 #endif /* _NO_EXTENSIONS */
1882
1883                 default:
1884                         return DefFrameProc(hwnd, Globals.hmdiclient, nmsg, wparam, lparam);
1885         }
1886
1887         return 0;
1888 }
1889
1890
1891 static const LPTSTR g_pos_names[COLUMNS] = {
1892         TEXT(""),                       /* symbol */
1893         TEXT("Name"),
1894         TEXT("Size"),
1895         TEXT("CDate"),
1896 #ifndef _NO_EXTENSIONS
1897         TEXT("ADate"),
1898         TEXT("MDate"),
1899         TEXT("Index/Inode"),
1900         TEXT("Links"),
1901 #endif /* _NO_EXTENSIONS */
1902         TEXT("Attributes"),
1903 #ifndef _NO_EXTENSIONS
1904         TEXT("Security")
1905 #endif
1906 };
1907
1908 static const int g_pos_align[] = {
1909         0,
1910         HDF_LEFT,       /* Name */
1911         HDF_RIGHT,      /* Size */
1912         HDF_LEFT,       /* CDate */
1913 #ifndef _NO_EXTENSIONS
1914         HDF_LEFT,       /* ADate */
1915         HDF_LEFT,       /* MDate */
1916         HDF_LEFT,       /* Index */
1917         HDF_CENTER,     /* Links */
1918 #endif
1919         HDF_CENTER,     /* Attributes */
1920 #ifndef _NO_EXTENSIONS
1921         HDF_LEFT        /* Security */
1922 #endif
1923 };
1924
1925 static void resize_tree(ChildWnd* child, int cx, int cy)
1926 {
1927         HDWP hdwp = BeginDeferWindowPos(4);
1928         RECT rt;
1929
1930         rt.left   = 0;
1931         rt.top    = 0;
1932         rt.right  = cx;
1933         rt.bottom = cy;
1934
1935         cx = child->split_pos + SPLIT_WIDTH/2;
1936
1937 #ifndef _NO_EXTENSIONS
1938         {
1939                 WINDOWPOS wp;
1940                 HD_LAYOUT hdl;
1941
1942                 hdl.prc   = &rt;
1943                 hdl.pwpos = &wp;
1944
1945                 Header_Layout(child->left.hwndHeader, &hdl);
1946
1947                 DeferWindowPos(hdwp, child->left.hwndHeader, wp.hwndInsertAfter,
1948                                                 wp.x-1, wp.y, child->split_pos-SPLIT_WIDTH/2+1, wp.cy, wp.flags);
1949                 DeferWindowPos(hdwp, child->right.hwndHeader, wp.hwndInsertAfter,
1950                                                 rt.left+cx+1, wp.y, wp.cx-cx+2, wp.cy, wp.flags);
1951         }
1952 #endif /* _NO_EXTENSIONS */
1953
1954         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);
1955         DeferWindowPos(hdwp, child->right.hwnd, 0, rt.left+cx+1, rt.top, rt.right-cx, rt.bottom-rt.top, SWP_NOZORDER|SWP_NOACTIVATE);
1956
1957         EndDeferWindowPos(hdwp);
1958 }
1959
1960
1961 #ifndef _NO_EXTENSIONS
1962
1963 static HWND create_header(HWND parent, Pane* pane, int id)
1964 {
1965         HD_ITEM hdi;
1966         int idx;
1967
1968         HWND hwnd = CreateWindow(WC_HEADER, 0, WS_CHILD|WS_VISIBLE|HDS_HORZ/*TODO: |HDS_BUTTONS + sort orders*/,
1969                                                                 0, 0, 0, 0, parent, (HMENU)id, Globals.hInstance, 0);
1970         if (!hwnd)
1971                 return 0;
1972
1973         SendMessage(hwnd, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), FALSE);
1974
1975         hdi.mask = HDI_TEXT|HDI_WIDTH|HDI_FORMAT;
1976
1977         for(idx=0; idx<COLUMNS; idx++) {
1978                 hdi.pszText = g_pos_names[idx];
1979                 hdi.fmt = HDF_STRING | g_pos_align[idx];
1980                 hdi.cxy = pane->widths[idx];
1981                 Header_InsertItem(hwnd, idx, &hdi);
1982         }
1983
1984         return hwnd;
1985 }
1986
1987 #endif /* _NO_EXTENSIONS */
1988
1989
1990 static void init_output(HWND hwnd)
1991 {
1992         TCHAR b[16];
1993         HFONT old_font;
1994         HDC hdc = GetDC(hwnd);
1995
1996         if (GetNumberFormat(LOCALE_USER_DEFAULT, 0, TEXT("1000"), 0, b, 16) > 4)
1997                 Globals.num_sep = b[1];
1998         else
1999                 Globals.num_sep = TEXT('.');
2000
2001         old_font = SelectFont(hdc, Globals.hfont);
2002         GetTextExtentPoint32(hdc, TEXT(" "), 1, &Globals.spaceSize);
2003         SelectFont(hdc, old_font);
2004         ReleaseDC(hwnd, hdc);
2005 }
2006
2007 static void draw_item(Pane* pane, LPDRAWITEMSTRUCT dis, Entry* entry, int calcWidthCol);
2008
2009
2010 /* calculate prefered width for all visible columns */
2011
2012 static BOOL calc_widths(Pane* pane, BOOL anyway)
2013 {
2014         int col, x, cx, spc=3*Globals.spaceSize.cx;
2015         int entries = ListBox_GetCount(pane->hwnd);
2016         int orgWidths[COLUMNS];
2017         int orgPositions[COLUMNS+1];
2018         HFONT hfontOld;
2019         HDC hdc;
2020         int cnt;
2021
2022         if (!anyway) {
2023                 memcpy(orgWidths, pane->widths, sizeof(orgWidths));
2024                 memcpy(orgPositions, pane->positions, sizeof(orgPositions));
2025         }
2026
2027         for(col=0; col<COLUMNS; col++)
2028                 pane->widths[col] = 0;
2029
2030         hdc = GetDC(pane->hwnd);
2031         hfontOld = SelectFont(hdc, Globals.hfont);
2032
2033         for(cnt=0; cnt<entries; cnt++) {
2034                 Entry* entry = (Entry*) ListBox_GetItemData(pane->hwnd, cnt);
2035
2036                 DRAWITEMSTRUCT dis;
2037
2038                 dis.CtlType               = 0;
2039                 dis.CtlID                 = 0;
2040                 dis.itemID                = 0;
2041                 dis.itemAction    = 0;
2042                 dis.itemState     = 0;
2043                 dis.hwndItem      = pane->hwnd;
2044                 dis.hDC                   = hdc;
2045                 dis.rcItem.left   = 0;
2046                 dis.rcItem.top    = 0;
2047                 dis.rcItem.right  = 0;
2048                 dis.rcItem.bottom = 0;
2049                 /*dis.itemData    = 0; */
2050
2051                 draw_item(pane, &dis, entry, COLUMNS);
2052         }
2053
2054         SelectObject(hdc, hfontOld);
2055         ReleaseDC(pane->hwnd, hdc);
2056
2057         x = 0;
2058         for(col=0; col<COLUMNS; col++) {
2059                 pane->positions[col] = x;
2060                 cx = pane->widths[col];
2061
2062                 if (cx) {
2063                         cx += spc;
2064
2065                         if (cx < IMAGE_WIDTH)
2066                                 cx = IMAGE_WIDTH;
2067
2068                         pane->widths[col] = cx;
2069                 }
2070
2071                 x += cx;
2072         }
2073
2074         pane->positions[COLUMNS] = x;
2075
2076         ListBox_SetHorizontalExtent(pane->hwnd, x);
2077
2078         /* no change? */
2079         if (!memcmp(orgWidths, pane->widths, sizeof(orgWidths)))
2080                 return FALSE;
2081
2082         /* don't move, if only collapsing an entry */
2083         if (!anyway && pane->widths[0]<orgWidths[0] &&
2084                 !memcmp(orgWidths+1, pane->widths+1, sizeof(orgWidths)-sizeof(int))) {
2085                 pane->widths[0] = orgWidths[0];
2086                 memcpy(pane->positions, orgPositions, sizeof(orgPositions));
2087
2088                 return FALSE;
2089         }
2090
2091         InvalidateRect(pane->hwnd, 0, TRUE);
2092
2093         return TRUE;
2094 }
2095
2096
2097 /* calculate one prefered column width */
2098
2099 static void calc_single_width(Pane* pane, int col)
2100 {
2101         HFONT hfontOld;
2102         int x, cx;
2103         int entries = ListBox_GetCount(pane->hwnd);
2104         int cnt;
2105         HDC hdc;
2106
2107         pane->widths[col] = 0;
2108
2109         hdc = GetDC(pane->hwnd);
2110         hfontOld = SelectFont(hdc, Globals.hfont);
2111
2112         for(cnt=0; cnt<entries; cnt++) {
2113                 Entry* entry = (Entry*) ListBox_GetItemData(pane->hwnd, cnt);
2114                 DRAWITEMSTRUCT dis;
2115
2116                 dis.CtlType               = 0;
2117                 dis.CtlID                 = 0;
2118                 dis.itemID                = 0;
2119                 dis.itemAction    = 0;
2120                 dis.itemState     = 0;
2121                 dis.hwndItem      = pane->hwnd;
2122                 dis.hDC                   = hdc;
2123                 dis.rcItem.left   = 0;
2124                 dis.rcItem.top    = 0;
2125                 dis.rcItem.right  = 0;
2126                 dis.rcItem.bottom = 0;
2127                 /*dis.itemData    = 0; */
2128
2129                 draw_item(pane, &dis, entry, col);
2130         }
2131
2132         SelectObject(hdc, hfontOld);
2133         ReleaseDC(pane->hwnd, hdc);
2134
2135         cx = pane->widths[col];
2136
2137         if (cx) {
2138                 cx += 3*Globals.spaceSize.cx;
2139
2140                 if (cx < IMAGE_WIDTH)
2141                         cx = IMAGE_WIDTH;
2142         }
2143
2144         pane->widths[col] = cx;
2145
2146         x = pane->positions[col] + cx;
2147
2148         for(; col<COLUMNS; ) {
2149                 pane->positions[++col] = x;
2150                 x += pane->widths[col];
2151         }
2152
2153         ListBox_SetHorizontalExtent(pane->hwnd, x);
2154 }
2155
2156
2157 /* insert listbox entries after index idx */
2158
2159 static void insert_entries(Pane* pane, Entry* dir, int idx)
2160 {
2161         Entry* entry = dir;
2162
2163         if (!entry)
2164                 return;
2165
2166         ShowWindow(pane->hwnd, SW_HIDE);
2167
2168         for(; entry; entry=entry->next) {
2169 #ifndef _LEFT_FILES
2170                 if (pane->treePane && !(entry->data.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY))
2171                         continue;
2172 #endif
2173
2174                 /* don't display entries "." and ".." in the left pane */
2175                 if (pane->treePane && (entry->data.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
2176                                 && entry->data.cFileName[0]==TEXT('.'))
2177                         if (
2178 #ifndef _NO_EXTENSIONS
2179                                 entry->data.cFileName[1]==TEXT('\0') ||
2180 #endif
2181                                 (entry->data.cFileName[1]==TEXT('.') && entry->data.cFileName[2]==TEXT('\0')))
2182                                 continue;
2183
2184                 if (idx != -1)
2185                         idx++;
2186
2187                 ListBox_InsertItemData(pane->hwnd, idx, entry);
2188
2189                 if (pane->treePane && entry->expanded)
2190                         insert_entries(pane, entry->down, idx);
2191         }
2192
2193         ShowWindow(pane->hwnd, SW_SHOW);
2194 }
2195
2196
2197 static WNDPROC g_orgTreeWndProc;
2198
2199 static void create_tree_window(HWND parent, Pane* pane, int id, int id_header)
2200 {
2201         static int s_init = 0;
2202         Entry* entry = pane->root;
2203
2204         pane->hwnd = CreateWindow(TEXT("ListBox"), TEXT(""), WS_CHILD|WS_VISIBLE|WS_HSCROLL|WS_VSCROLL|
2205                                                                 LBS_DISABLENOSCROLL|LBS_NOINTEGRALHEIGHT|LBS_OWNERDRAWFIXED|LBS_NOTIFY,
2206                                                                 0, 0, 0, 0, parent, (HMENU)id, Globals.hInstance, 0);
2207
2208         SetWindowLong(pane->hwnd, GWL_USERDATA, (LPARAM)pane);
2209         g_orgTreeWndProc = SubclassWindow(pane->hwnd, TreeWndProc);
2210
2211         SendMessage(pane->hwnd, WM_SETFONT, (WPARAM)Globals.hfont, FALSE);
2212
2213         /* insert entries into listbox */
2214         if (entry)
2215                 insert_entries(pane, entry, -1);
2216
2217         /* calculate column widths */
2218         if (!s_init) {
2219                 s_init = 1;
2220                 init_output(pane->hwnd);
2221         }
2222
2223         calc_widths(pane, TRUE);
2224
2225 #ifndef _NO_EXTENSIONS
2226         pane->hwndHeader = create_header(parent, pane, id_header);
2227 #endif
2228 }
2229
2230
2231 static void InitChildWindow(ChildWnd* child)
2232 {
2233         create_tree_window(child->hwnd, &child->left, IDW_TREE_LEFT, IDW_HEADER_LEFT);
2234         create_tree_window(child->hwnd, &child->right, IDW_TREE_RIGHT, IDW_HEADER_RIGHT);
2235 }
2236
2237
2238 static void format_date(const FILETIME* ft, TCHAR* buffer, int visible_cols)
2239 {
2240         SYSTEMTIME systime;
2241         FILETIME lft;
2242         int len = 0;
2243
2244         *buffer = TEXT('\0');
2245
2246         if (!ft->dwLowDateTime && !ft->dwHighDateTime)
2247                 return;
2248
2249         if (!FileTimeToLocalFileTime(ft, &lft))
2250                 {err: _tcscpy(buffer,TEXT("???")); return;}
2251
2252         if (!FileTimeToSystemTime(&lft, &systime))
2253                 goto err;
2254
2255         if (visible_cols & COL_DATE) {
2256                 len = GetDateFormat(LOCALE_USER_DEFAULT, 0, &systime, 0, buffer, BUFFER_LEN);
2257                 if (!len)
2258                         goto err;
2259         }
2260
2261         if (visible_cols & COL_TIME) {
2262                 if (len)
2263                         buffer[len-1] = ' ';
2264
2265                 buffer[len++] = ' ';
2266
2267                 if (!GetTimeFormat(LOCALE_USER_DEFAULT, 0, &systime, 0, buffer+len, BUFFER_LEN-len))
2268                         buffer[len] = TEXT('\0');
2269         }
2270 }
2271
2272
2273 static void calc_width(Pane* pane, LPDRAWITEMSTRUCT dis, int col, LPCTSTR str)
2274 {
2275         RECT rt = {0, 0, 0, 0};
2276
2277         DrawText(dis->hDC, (LPTSTR)str, -1, &rt, DT_CALCRECT|DT_SINGLELINE|DT_NOPREFIX);
2278
2279         if (rt.right > pane->widths[col])
2280                 pane->widths[col] = rt.right;
2281 }
2282
2283 static void calc_tabbed_width(Pane* pane, LPDRAWITEMSTRUCT dis, int col, LPCTSTR str)
2284 {
2285         RECT rt = {0, 0, 0, 0};
2286
2287 /*      DRAWTEXTPARAMS dtp = {sizeof(DRAWTEXTPARAMS), 2};
2288         DrawTextEx(dis->hDC, (LPTSTR)str, -1, &rt, DT_CALCRECT|DT_SINGLELINE|DT_NOPREFIX|DT_EXPANDTABS|DT_TABSTOP, &dtp);*/
2289
2290         DrawText(dis->hDC, (LPTSTR)str, -1, &rt, DT_CALCRECT|DT_SINGLELINE|DT_EXPANDTABS|DT_TABSTOP|(2<<8));
2291         /*FIXME rt (0,0) ??? */
2292
2293         if (rt.right > pane->widths[col])
2294                 pane->widths[col] = rt.right;
2295 }
2296
2297
2298 static void output_text(Pane* pane, LPDRAWITEMSTRUCT dis, int col, LPCTSTR str, DWORD flags)
2299 {
2300         int x = dis->rcItem.left;
2301         RECT rt;
2302
2303         rt.left   = x+pane->positions[col]+Globals.spaceSize.cx;
2304         rt.top    = dis->rcItem.top;
2305         rt.right  = x+pane->positions[col+1]-Globals.spaceSize.cx;
2306         rt.bottom = dis->rcItem.bottom;
2307
2308         DrawText(dis->hDC, (LPTSTR)str, -1, &rt, DT_SINGLELINE|DT_NOPREFIX|flags);
2309 }
2310
2311 static void output_tabbed_text(Pane* pane, LPDRAWITEMSTRUCT dis, int col, LPCTSTR str)
2312 {
2313         int x = dis->rcItem.left;
2314         RECT rt;
2315
2316         rt.left   = x+pane->positions[col]+Globals.spaceSize.cx;
2317         rt.top    = dis->rcItem.top;
2318         rt.right  = x+pane->positions[col+1]-Globals.spaceSize.cx;
2319         rt.bottom = dis->rcItem.bottom;
2320
2321 /*      DRAWTEXTPARAMS dtp = {sizeof(DRAWTEXTPARAMS), 2};
2322         DrawTextEx(dis->hDC, (LPTSTR)str, -1, &rt, DT_SINGLELINE|DT_NOPREFIX|DT_EXPANDTABS|DT_TABSTOP, &dtp);*/
2323
2324         DrawText(dis->hDC, (LPTSTR)str, -1, &rt, DT_SINGLELINE|DT_EXPANDTABS|DT_TABSTOP|(2<<8));
2325 }
2326
2327 static void output_number(Pane* pane, LPDRAWITEMSTRUCT dis, int col, LPCTSTR str)
2328 {
2329         int x = dis->rcItem.left;
2330         RECT rt;
2331         LPCTSTR s = str;
2332         TCHAR b[128];
2333         LPTSTR d = b;
2334         int pos;
2335
2336         rt.left   = x+pane->positions[col]+Globals.spaceSize.cx;
2337         rt.top    = dis->rcItem.top;
2338         rt.right  = x+pane->positions[col+1]-Globals.spaceSize.cx;
2339         rt.bottom = dis->rcItem.bottom;
2340
2341         if (*s)
2342                 *d++ = *s++;
2343
2344         /* insert number separator characters */
2345         pos = lstrlen(s) % 3;
2346
2347         while(*s)
2348                 if (pos--)
2349                         *d++ = *s++;
2350                 else {
2351                         *d++ = Globals.num_sep;
2352                         pos = 3;
2353                 }
2354
2355         DrawText(dis->hDC, b, d-b, &rt, DT_RIGHT|DT_SINGLELINE|DT_NOPREFIX|DT_END_ELLIPSIS);
2356 }
2357
2358
2359 static int is_exe_file(LPCTSTR ext)
2360 {
2361         static const LPCTSTR executable_extensions[] = {
2362                 TEXT("COM"),
2363                 TEXT("EXE"),
2364                 TEXT("BAT"),
2365                 TEXT("CMD"),
2366 #ifndef _NO_EXTENSIONS
2367                 TEXT("CMM"),
2368                 TEXT("BTM"),
2369                 TEXT("AWK"),
2370 #endif /* _NO_EXTENSIONS */
2371                 0
2372         };
2373
2374         TCHAR ext_buffer[_MAX_EXT];
2375         const LPCTSTR* p;
2376         LPCTSTR s;
2377         LPTSTR d;
2378
2379         for(s=ext+1,d=ext_buffer; (*d=tolower(*s)); s++)
2380                 d++;
2381
2382         for(p=executable_extensions; *p; p++)
2383                 if (!_tcscmp(ext_buffer, *p))
2384                         return 1;
2385
2386         return 0;
2387 }
2388
2389 static int is_registered_type(LPCTSTR ext)
2390 {
2391         /* TODO */
2392
2393         return 1;
2394 }
2395
2396
2397 static void draw_item(Pane* pane, LPDRAWITEMSTRUCT dis, Entry* entry, int calcWidthCol)
2398 {
2399         TCHAR buffer[BUFFER_LEN];
2400         DWORD attrs;
2401         int visible_cols = pane->visible_cols;
2402         COLORREF bkcolor, textcolor;
2403         RECT focusRect = dis->rcItem;
2404         HBRUSH hbrush;
2405         enum IMAGE img;
2406         int img_pos, cx;
2407         int col = 0;
2408
2409         if (entry) {
2410                 attrs = entry->data.dwFileAttributes;
2411
2412                 if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
2413                         if (entry->data.cFileName[0]==TEXT('.') && entry->data.cFileName[1]==TEXT('.')
2414                                         && entry->data.cFileName[2]==TEXT('\0'))
2415                                 img = IMG_FOLDER_UP;
2416 #ifndef _NO_EXTENSIONS
2417                         else if (entry->data.cFileName[0]==TEXT('.') && entry->data.cFileName[1]==TEXT('\0'))
2418                                 img = IMG_FOLDER_CUR;
2419 #endif
2420                         else if (
2421 #ifdef _NO_EXTENSIONS
2422                                          entry->expanded ||
2423 #endif
2424                                          (pane->treePane && (dis->itemState&ODS_FOCUS)))
2425                                 img = IMG_OPEN_FOLDER;
2426                         else
2427                                 img = IMG_FOLDER;
2428                 } else {
2429                         LPCTSTR ext = _tcsrchr(entry->data.cFileName, '.');
2430                         if (!ext)
2431                                 ext = TEXT("");
2432
2433                         if (is_exe_file(ext))
2434                                 img = IMG_EXECUTABLE;
2435                         else if (is_registered_type(ext))
2436                                 img = IMG_DOCUMENT;
2437                         else
2438                                 img = IMG_FILE;
2439                 }
2440         } else {
2441                 attrs = 0;
2442                 img = IMG_NONE;
2443         }
2444
2445         if (pane->treePane) {
2446                 if (entry) {
2447                         img_pos = dis->rcItem.left + entry->level*(IMAGE_WIDTH+Globals.spaceSize.cx);
2448
2449                         if (calcWidthCol == -1) {
2450                                 int x;
2451                                 int y = dis->rcItem.top + IMAGE_HEIGHT/2;
2452                                 Entry* up;
2453                                 RECT rt_clip;
2454                                 HRGN hrgn_org = CreateRectRgn(0, 0, 0, 0);
2455                                 HRGN hrgn;
2456
2457                                 rt_clip.left   = dis->rcItem.left;
2458                                 rt_clip.top    = dis->rcItem.top;
2459                                 rt_clip.right  = dis->rcItem.left+pane->widths[col];
2460                                 rt_clip.bottom = dis->rcItem.bottom;
2461
2462                                 hrgn = CreateRectRgnIndirect(&rt_clip);
2463
2464                                 if (!GetClipRgn(dis->hDC, hrgn_org)) {
2465                                         DeleteObject(hrgn_org);
2466                                         hrgn_org = 0;
2467                                 }
2468
2469                                 /* HGDIOBJ holdPen = SelectObject(dis->hDC, GetStockObject(BLACK_PEN)); */
2470                                 ExtSelectClipRgn(dis->hDC, hrgn, RGN_AND);
2471                                 DeleteObject(hrgn);
2472
2473                                 if ((up=entry->up) != NULL) {
2474                                         MoveToEx(dis->hDC, img_pos-IMAGE_WIDTH/2, y, 0);
2475                                         LineTo(dis->hDC, img_pos-2, y);
2476
2477                                         x = img_pos - IMAGE_WIDTH/2;
2478
2479                                         do {
2480                                                 x -= IMAGE_WIDTH+Globals.spaceSize.cx;
2481
2482                                                 if (up->next
2483 #ifndef _LEFT_FILES
2484                                                         && (up->next->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
2485 #endif
2486                                                         ) {
2487                                                         MoveToEx(dis->hDC, x, dis->rcItem.top, 0);
2488                                                         LineTo(dis->hDC, x, dis->rcItem.bottom);
2489                                                 }
2490                                         } while((up=up->up) != NULL);
2491                                 }
2492
2493                                 x = img_pos - IMAGE_WIDTH/2;
2494
2495                                 MoveToEx(dis->hDC, x, dis->rcItem.top, 0);
2496                                 LineTo(dis->hDC, x, y);
2497
2498                                 if (entry->next
2499 #ifndef _LEFT_FILES
2500                                         && (entry->next->data.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
2501 #endif
2502                                         )
2503                                         LineTo(dis->hDC, x, dis->rcItem.bottom);
2504
2505                                 if (entry->down && entry->expanded) {
2506                                         x += IMAGE_WIDTH+Globals.spaceSize.cx;
2507                                         MoveToEx(dis->hDC, x, dis->rcItem.top+IMAGE_HEIGHT, 0);
2508                                         LineTo(dis->hDC, x, dis->rcItem.bottom);
2509                                 }
2510
2511                                 SelectClipRgn(dis->hDC, hrgn_org);
2512                                 if (hrgn_org) DeleteObject(hrgn_org);
2513                                 /* SelectObject(dis->hDC, holdPen); */
2514                         } else if (calcWidthCol==col || calcWidthCol==COLUMNS) {
2515                                 int right = img_pos + IMAGE_WIDTH - Globals.spaceSize.cx;
2516
2517                                 if (right > pane->widths[col])
2518                                         pane->widths[col] = right;
2519                         }
2520                 } else  {
2521                         img_pos = dis->rcItem.left;
2522                 }
2523         } else {
2524                 img_pos = dis->rcItem.left;
2525
2526                 if (calcWidthCol==col || calcWidthCol==COLUMNS)
2527                         pane->widths[col] = IMAGE_WIDTH;
2528         }
2529
2530         if (calcWidthCol == -1) {
2531                 focusRect.left = img_pos -2;
2532
2533 #ifdef _NO_EXTENSIONS
2534                 if (pane->treePane && entry) {
2535                         RECT rt = {0};
2536
2537                         DrawText(dis->hDC, entry->data.cFileName, -1, &rt, DT_CALCRECT|DT_SINGLELINE|DT_NOPREFIX);
2538
2539                         focusRect.right = dis->rcItem.left+pane->positions[col+1]+Globals.spaceSize.cx + rt.right +2;
2540                 }
2541 #else
2542
2543                 if (attrs & FILE_ATTRIBUTE_COMPRESSED)
2544                         textcolor = COLOR_COMPRESSED;
2545                 else
2546 #endif /* _NO_EXTENSIONS */
2547                         textcolor = RGB(0,0,0);
2548
2549                 if (dis->itemState & ODS_FOCUS) {
2550                         textcolor = RGB(255,255,255);
2551                         bkcolor = COLOR_SELECTION;
2552                 } else {
2553                         bkcolor = RGB(255,255,255);
2554                 }
2555
2556                 hbrush = CreateSolidBrush(bkcolor);
2557                 FillRect(dis->hDC, &focusRect, hbrush);
2558                 DeleteObject(hbrush);
2559
2560                 SetBkMode(dis->hDC, TRANSPARENT);
2561                 SetTextColor(dis->hDC, textcolor);
2562
2563                 cx = pane->widths[col];
2564
2565                 if (cx && img!=IMG_NONE) {
2566                         if (cx > IMAGE_WIDTH)
2567                                 cx = IMAGE_WIDTH;
2568
2569 #ifdef _SHELL_FOLDERS
2570                         if (entry->hicon && entry->hicon!=(HICON)-1)
2571                                 DrawIconEx(dis->hDC, img_pos, dis->rcItem.top, entry->hicon, cx, GetSystemMetrics(SM_CYSMICON), 0, 0, DI_NORMAL);
2572                         else
2573 #endif
2574                                 ImageList_DrawEx(Globals.himl, img, dis->hDC,
2575                                                                  img_pos, dis->rcItem.top, cx,
2576                                                                  IMAGE_HEIGHT, bkcolor, CLR_DEFAULT, ILD_NORMAL);
2577                 }
2578         }
2579
2580         if (!entry)
2581                 return;
2582
2583 #ifdef _NO_EXTENSIONS
2584         if (img >= IMG_FOLDER_UP)
2585                 return;
2586 #endif
2587
2588         col++;
2589
2590         /* ouput file name */
2591         if (calcWidthCol == -1)
2592                 output_text(pane, dis, col, entry->data.cFileName, 0);
2593         else if (calcWidthCol==col || calcWidthCol==COLUMNS)
2594                 calc_width(pane, dis, col, entry->data.cFileName);
2595
2596         col++;
2597
2598 #ifdef _NO_EXTENSIONS
2599   if (!pane->treePane) {
2600 #endif
2601
2602         /* display file size */
2603         if (visible_cols & COL_SIZE) {
2604 #ifdef _NO_EXTENSIONS
2605                 if (!(attrs&FILE_ATTRIBUTE_DIRECTORY))
2606 #endif
2607                 {
2608                         ULONGLONG size;
2609
2610                         size = ((ULONGLONG)entry->data.nFileSizeHigh << 32) | entry->data.nFileSizeLow;
2611
2612                         _stprintf(buffer, TEXT("%") LONGLONGARG TEXT("d"), size);
2613
2614                         if (calcWidthCol == -1)
2615                                 output_number(pane, dis, col, buffer);
2616                         else if (calcWidthCol==col || calcWidthCol==COLUMNS)
2617                                 calc_width(pane, dis, col, buffer);/*TODO: not ever time enough */
2618                 }
2619
2620                 col++;
2621         }
2622
2623         /* display file date */
2624         if (visible_cols & (COL_DATE|COL_TIME)) {
2625 #ifndef _NO_EXTENSIONS
2626                 format_date(&entry->data.ftCreationTime, buffer, visible_cols);
2627                 if (calcWidthCol == -1)
2628                         output_text(pane, dis, col, buffer, 0);
2629                 else if (calcWidthCol==col || calcWidthCol==COLUMNS)
2630                         calc_width(pane, dis, col, buffer);
2631                 col++;
2632
2633                 format_date(&entry->data.ftLastAccessTime, buffer, visible_cols);
2634                 if (calcWidthCol == -1)
2635                         output_text(pane, dis, col, buffer, 0);
2636                 else if (calcWidthCol==col || calcWidthCol==COLUMNS)
2637                         calc_width(pane, dis, col, buffer);
2638                 col++;
2639 #endif /* _NO_EXTENSIONS */
2640
2641                 format_date(&entry->data.ftLastWriteTime, buffer, visible_cols);
2642                 if (calcWidthCol == -1)
2643                         output_text(pane, dis, col, buffer, 0);
2644                 else if (calcWidthCol==col || calcWidthCol==COLUMNS)
2645                         calc_width(pane, dis, col, buffer);
2646                 col++;
2647         }
2648
2649 #ifndef _NO_EXTENSIONS
2650         if (entry->bhfi_valid) {
2651             ULONGLONG index = ((ULONGLONG)entry->bhfi.nFileIndexHigh << 32) | entry->bhfi.nFileIndexLow;
2652
2653                 if (visible_cols & COL_INDEX) {
2654                         _stprintf(buffer, TEXT("%") LONGLONGARG TEXT("X"), index);
2655                         if (calcWidthCol == -1)
2656                                 output_text(pane, dis, col, buffer, DT_RIGHT);
2657                         else if (calcWidthCol==col || calcWidthCol==COLUMNS)
2658                                 calc_width(pane, dis, col, buffer);
2659                         col++;
2660                 }
2661
2662                 if (visible_cols & COL_LINKS) {
2663                         wsprintf(buffer, TEXT("%d"), entry->bhfi.nNumberOfLinks);
2664                         if (calcWidthCol == -1)
2665                                 output_text(pane, dis, col, buffer, DT_CENTER);
2666                         else if (calcWidthCol==col || calcWidthCol==COLUMNS)
2667                                 calc_width(pane, dis, col, buffer);
2668                         col++;
2669                 }
2670         } else
2671                 col += 2;
2672 #endif /* _NO_EXTENSIONS */
2673
2674         /* show file attributes */
2675         if (visible_cols & COL_ATTRIBUTES) {
2676 #ifdef _NO_EXTENSIONS
2677                 _tcscpy(buffer, TEXT(" \t \t \t \t "));
2678 #else
2679                 _tcscpy(buffer, TEXT(" \t \t \t \t \t \t \t \t \t \t \t "));
2680 #endif
2681
2682                 if (attrs & FILE_ATTRIBUTE_NORMAL)                                      buffer[ 0] = 'N';
2683                 else {
2684                         if (attrs & FILE_ATTRIBUTE_READONLY)                    buffer[ 2] = 'R';
2685                         if (attrs & FILE_ATTRIBUTE_HIDDEN)                              buffer[ 4] = 'H';
2686                         if (attrs & FILE_ATTRIBUTE_SYSTEM)                              buffer[ 6] = 'S';
2687                         if (attrs & FILE_ATTRIBUTE_ARCHIVE)                             buffer[ 8] = 'A';
2688                         if (attrs & FILE_ATTRIBUTE_COMPRESSED)                  buffer[10] = 'C';
2689 #ifndef _NO_EXTENSIONS
2690                         if (attrs & FILE_ATTRIBUTE_DIRECTORY)                   buffer[12] = 'D';
2691                         if (attrs & FILE_ATTRIBUTE_ENCRYPTED)                   buffer[14] = 'E';
2692                         if (attrs & FILE_ATTRIBUTE_TEMPORARY)                   buffer[16] = 'T';
2693                         if (attrs & FILE_ATTRIBUTE_SPARSE_FILE)                 buffer[18] = 'P';
2694                         if (attrs & FILE_ATTRIBUTE_REPARSE_POINT)               buffer[20] = 'Q';
2695                         if (attrs & FILE_ATTRIBUTE_OFFLINE)                             buffer[22] = 'O';
2696                         if (attrs & FILE_ATTRIBUTE_NOT_CONTENT_INDEXED) buffer[24] = 'X';
2697 #endif /* _NO_EXTENSIONS */
2698                 }
2699
2700                 if (calcWidthCol == -1)
2701                         output_tabbed_text(pane, dis, col, buffer);
2702                 else if (calcWidthCol==col || calcWidthCol==COLUMNS)
2703                         calc_tabbed_width(pane, dis, col, buffer);
2704
2705                 col++;
2706         }
2707
2708 /*TODO
2709         if (flags.security) {
2710                 DWORD rights = get_access_mask();
2711
2712                 tcscpy(buffer, TEXT(" \t \t \t  \t  \t \t \t  \t  \t \t \t "));
2713
2714                 if (rights & FILE_READ_DATA)                    buffer[ 0] = 'R';
2715                 if (rights & FILE_WRITE_DATA)                   buffer[ 2] = 'W';
2716                 if (rights & FILE_APPEND_DATA)                  buffer[ 4] = 'A';
2717                 if (rights & FILE_READ_EA)                              {buffer[6] = 'entry'; buffer[ 7] = 'R';}
2718                 if (rights & FILE_WRITE_EA)                             {buffer[9] = 'entry'; buffer[10] = 'W';}
2719                 if (rights & FILE_EXECUTE)                              buffer[12] = 'X';
2720                 if (rights & FILE_DELETE_CHILD)                 buffer[14] = 'D';
2721                 if (rights & FILE_READ_ATTRIBUTES)              {buffer[16] = 'a'; buffer[17] = 'R';}
2722                 if (rights & FILE_WRITE_ATTRIBUTES)             {buffer[19] = 'a'; buffer[20] = 'W';}
2723                 if (rights & WRITE_DAC)                                 buffer[22] = 'C';
2724                 if (rights & WRITE_OWNER)                               buffer[24] = 'O';
2725                 if (rights & SYNCHRONIZE)                               buffer[26] = 'S';
2726
2727                 output_text(dis, col++, buffer, DT_LEFT, 3, psize);
2728         }
2729
2730         if (flags.description) {
2731                 get_description(buffer);
2732                 output_text(dis, col++, buffer, 0, psize);
2733         }
2734 */
2735
2736 #ifdef _NO_EXTENSIONS
2737   }
2738
2739         /* draw focus frame */
2740         if ((dis->itemState&ODS_FOCUS) && calcWidthCol==-1) {
2741                 /* Currently [04/2000] Wine neither behaves exactly the same */
2742                 /* way as WIN 95 nor like Windows NT... */
2743                 HGDIOBJ lastBrush;
2744                 HPEN lastPen;
2745                 HPEN hpen;
2746
2747                 if (!(GetVersion() & 0x80000000)) {     /* Windows NT? */
2748                         LOGBRUSH lb = {PS_SOLID, RGB(255,255,255)};
2749                         hpen = ExtCreatePen(PS_COSMETIC|PS_ALTERNATE, 1, &lb, 0, 0);
2750                 } else
2751                         hpen = CreatePen(PS_DOT, 0, RGB(255,255,255));
2752
2753                 lastPen = SelectPen(dis->hDC, hpen);
2754                 lastBrush = SelectObject(dis->hDC, GetStockObject(HOLLOW_BRUSH));
2755                 SetROP2(dis->hDC, R2_XORPEN);
2756                 Rectangle(dis->hDC, focusRect.left, focusRect.top, focusRect.right, focusRect.bottom);
2757                 SelectObject(dis->hDC, lastBrush);
2758                 SelectObject(dis->hDC, lastPen);
2759                 DeleteObject(hpen);
2760         }
2761 #endif /* _NO_EXTENSIONS */
2762 }
2763
2764
2765 #ifdef _NO_EXTENSIONS
2766
2767 static void draw_splitbar(HWND hwnd, int x)
2768 {
2769         RECT rt;
2770         HDC hdc = GetDC(hwnd);
2771
2772         GetClientRect(hwnd, &rt);
2773
2774         rt.left = x - SPLIT_WIDTH/2;
2775         rt.right = x + SPLIT_WIDTH/2+1;
2776
2777         InvertRect(hdc, &rt);
2778
2779         ReleaseDC(hwnd, hdc);
2780 }
2781
2782 #endif /* _NO_EXTENSIONS */
2783
2784
2785 #ifndef _NO_EXTENSIONS
2786
2787 static void set_header(Pane* pane)
2788 {
2789         HD_ITEM item;
2790         int scroll_pos = GetScrollPos(pane->hwnd, SB_HORZ);
2791         int i=0, x=0;
2792
2793         item.mask = HDI_WIDTH;
2794         item.cxy = 0;
2795
2796         for(; x+pane->widths[i]<scroll_pos && i<COLUMNS; i++) {
2797                 x += pane->widths[i];
2798                 Header_SetItem(pane->hwndHeader, i, &item);
2799         }
2800
2801         if (i < COLUMNS) {
2802                 x += pane->widths[i];
2803                 item.cxy = x - scroll_pos;
2804                 Header_SetItem(pane->hwndHeader, i++, &item);
2805
2806                 for(; i<COLUMNS; i++) {
2807                         item.cxy = pane->widths[i];
2808                         x += pane->widths[i];
2809                         Header_SetItem(pane->hwndHeader, i, &item);
2810                 }
2811         }
2812 }
2813
2814 static LRESULT pane_notify(Pane* pane, NMHDR* pnmh)
2815 {
2816         switch(pnmh->code) {
2817                 case HDN_TRACK:
2818                 case HDN_ENDTRACK: {
2819                         HD_NOTIFY* phdn = (HD_NOTIFY*) pnmh;
2820                         int idx = phdn->iItem;
2821                         int dx = phdn->pitem->cxy - pane->widths[idx];
2822                         int i;
2823
2824                         RECT clnt;
2825                         GetClientRect(pane->hwnd, &clnt);
2826
2827                         /* move immediate to simulate HDS_FULLDRAG (for now [04/2000] not really needed with WINELIB) */
2828                         Header_SetItem(pane->hwndHeader, idx, phdn->pitem);
2829
2830                         pane->widths[idx] += dx;
2831
2832                         for(i=idx; ++i<=COLUMNS; )
2833                                 pane->positions[i] += dx;
2834
2835                         {
2836                                 int scroll_pos = GetScrollPos(pane->hwnd, SB_HORZ);
2837                                 RECT rt_scr;
2838                                 RECT rt_clip;
2839
2840                                 rt_scr.left   = pane->positions[idx+1]-scroll_pos;
2841                                 rt_scr.top    = 0;
2842                                 rt_scr.right  = clnt.right;
2843                                 rt_scr.bottom = clnt.bottom;
2844
2845                                 rt_clip.left   = pane->positions[idx]-scroll_pos;
2846                                 rt_clip.top    = 0;
2847                                 rt_clip.right  = clnt.right;
2848                                 rt_clip.bottom = clnt.bottom;
2849
2850                                 if (rt_scr.left < 0) rt_scr.left = 0;
2851                                 if (rt_clip.left < 0) rt_clip.left = 0;
2852
2853                                 ScrollWindowEx(pane->hwnd, dx, 0, &rt_scr, &rt_clip, 0, 0, SW_INVALIDATE);
2854
2855                                 rt_clip.right = pane->positions[idx+1];
2856                                 RedrawWindow(pane->hwnd, &rt_clip, 0, RDW_INVALIDATE|RDW_UPDATENOW);
2857
2858                                 if (pnmh->code == HDN_ENDTRACK) {
2859                                         ListBox_SetHorizontalExtent(pane->hwnd, pane->positions[COLUMNS]);
2860
2861                                         if (GetScrollPos(pane->hwnd, SB_HORZ) != scroll_pos)
2862                                                 set_header(pane);
2863                                 }
2864                         }
2865
2866                         return FALSE;
2867                 }
2868
2869                 case HDN_DIVIDERDBLCLICK: {
2870                         HD_NOTIFY* phdn = (HD_NOTIFY*) pnmh;
2871                         HD_ITEM item;
2872
2873                         calc_single_width(pane, phdn->iItem);
2874                         item.mask = HDI_WIDTH;
2875                         item.cxy = pane->widths[phdn->iItem];
2876
2877                         Header_SetItem(pane->hwndHeader, phdn->iItem, &item);
2878                         InvalidateRect(pane->hwnd, 0, TRUE);
2879                         break;}
2880         }
2881
2882         return 0;
2883 }
2884
2885 #endif /* _NO_EXTENSIONS */
2886
2887
2888 static void scan_entry(ChildWnd* child, Entry* entry, HWND hwnd)
2889 {
2890         TCHAR path[MAX_PATH];
2891         int idx = ListBox_GetCurSel(child->left.hwnd);
2892         HCURSOR old_cursor = SetCursor(LoadCursor(0, IDC_WAIT));
2893
2894         /* delete sub entries in left pane */
2895         for(;;) {
2896                 LRESULT res = ListBox_GetItemData(child->left.hwnd, idx+1);
2897                 Entry* sub = (Entry*) res;
2898
2899                 if (res==LB_ERR || !sub || sub->level<=entry->level)
2900                         break;
2901
2902                 ListBox_DeleteString(child->left.hwnd, idx+1);
2903         }
2904
2905         /* empty right pane */
2906         ListBox_ResetContent(child->right.hwnd);
2907
2908         /* release memory */
2909         free_entries(entry);
2910
2911         /* read contents from disk */
2912 #ifdef _SHELL_FOLDERS
2913         if (entry->etype == ET_SHELL)
2914         {
2915                 read_directory(entry, NULL, child->sortOrder, hwnd);
2916         }
2917         else
2918 #endif
2919         {
2920                 get_path(entry, path);
2921                 read_directory(entry, path, child->sortOrder, hwnd);
2922         }
2923
2924         /* insert found entries in right pane */
2925         insert_entries(&child->right, entry->down, -1);
2926         calc_widths(&child->right, FALSE);
2927 #ifndef _NO_EXTENSIONS
2928         set_header(&child->right);
2929 #endif
2930
2931         child->header_wdths_ok = FALSE;
2932
2933         SetCursor(old_cursor);
2934 }
2935
2936
2937 /* expand a directory entry */
2938
2939 static BOOL expand_entry(ChildWnd* child, Entry* dir)
2940 {
2941         int idx;
2942         Entry* p;
2943
2944         if (!dir || dir->expanded || !dir->down)
2945                 return FALSE;
2946
2947         p = dir->down;
2948
2949         if (p->data.cFileName[0]=='.' && p->data.cFileName[1]=='\0' && p->next) {
2950                 p = p->next;
2951
2952                 if (p->data.cFileName[0]=='.' && p->data.cFileName[1]=='.' &&
2953                                 p->data.cFileName[2]=='\0' && p->next)
2954                         p = p->next;
2955         }
2956
2957         /* no subdirectories ? */
2958         if (!(p->data.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY))
2959                 return FALSE;
2960
2961         idx = ListBox_FindItemData(child->left.hwnd, 0, dir);
2962
2963         dir->expanded = TRUE;
2964
2965         /* insert entries in left pane */
2966         insert_entries(&child->left, p, idx);
2967
2968         if (!child->header_wdths_ok) {
2969                 if (calc_widths(&child->left, FALSE)) {
2970 #ifndef _NO_EXTENSIONS
2971                         set_header(&child->left);
2972 #endif
2973
2974                         child->header_wdths_ok = TRUE;
2975                 }
2976         }
2977
2978         return TRUE;
2979 }
2980
2981
2982 static void collapse_entry(Pane* pane, Entry* dir)
2983 {
2984         int idx = ListBox_FindItemData(pane->hwnd, 0, dir);
2985
2986         ShowWindow(pane->hwnd, SW_HIDE);
2987
2988         /* hide sub entries */
2989         for(;;) {
2990                 LRESULT res = ListBox_GetItemData(pane->hwnd, idx+1);
2991                 Entry* sub = (Entry*) res;
2992
2993                 if (res==LB_ERR || !sub || sub->level<=dir->level)
2994                         break;
2995
2996                 ListBox_DeleteString(pane->hwnd, idx+1);
2997         }
2998
2999         dir->expanded = FALSE;
3000
3001         ShowWindow(pane->hwnd, SW_SHOW);
3002 }
3003
3004
3005 static void set_curdir(ChildWnd* child, Entry* entry, HWND hwnd)
3006 {
3007         TCHAR path[MAX_PATH];
3008
3009         path[0] = '\0';
3010
3011         child->left.cur = entry;
3012         child->right.root = entry->down? entry->down: entry;
3013         child->right.cur = entry;
3014
3015         if (!entry->scanned)
3016                 scan_entry(child, entry, hwnd);
3017         else {
3018                 ListBox_ResetContent(child->right.hwnd);
3019                 insert_entries(&child->right, entry->down, -1);
3020                 calc_widths(&child->right, FALSE);
3021 #ifndef _NO_EXTENSIONS
3022                 set_header(&child->right);
3023 #endif
3024         }
3025
3026         get_path(entry, path);
3027         lstrcpy(child->path, path);
3028
3029         if (child->hwnd)        /* only change window title, if the window already exists */
3030                 SetWindowText(child->hwnd, path);
3031
3032         if (path[0])
3033                 SetCurrentDirectory(path);
3034 }
3035
3036
3037 BOOL launch_file(HWND hwnd, LPCTSTR cmd, UINT nCmdShow)
3038 {
3039         HINSTANCE hinst = ShellExecute(hwnd, NULL/*operation*/, cmd, NULL/*parameters*/, NULL/*dir*/, nCmdShow);
3040
3041         if ((int)hinst <= 32) {
3042                 display_error(hwnd, GetLastError());
3043                 return FALSE;
3044         }
3045
3046         return TRUE;
3047 }
3048
3049 #ifdef UNICODE
3050 BOOL launch_fileA(HWND hwnd, LPSTR cmd, UINT nCmdShow)
3051 {
3052         HINSTANCE hinst = ShellExecuteA(hwnd, NULL/*operation*/, cmd, NULL/*parameters*/, NULL/*dir*/, nCmdShow);
3053
3054         if ((int)hinst <= 32) {
3055                 display_error(hwnd, GetLastError());
3056                 return FALSE;
3057         }
3058
3059         return TRUE;
3060 }
3061 #endif
3062
3063
3064 BOOL launch_entry(Entry* entry, HWND hwnd, UINT nCmdShow)
3065 {
3066         TCHAR cmd[MAX_PATH];
3067
3068 #ifdef _SHELL_FOLDERS
3069         if (entry->etype == ET_SHELL) {
3070                 BOOL ret = TRUE;
3071
3072                 SHELLEXECUTEINFO shexinfo;
3073
3074                 shexinfo.cbSize = sizeof(SHELLEXECUTEINFO);
3075                 shexinfo.fMask = SEE_MASK_IDLIST;
3076                 shexinfo.hwnd = hwnd;
3077                 shexinfo.lpVerb = NULL;
3078                 shexinfo.lpFile = NULL;
3079                 shexinfo.lpParameters = NULL;
3080                 shexinfo.lpDirectory = NULL;
3081                 shexinfo.nShow = nCmdShow;
3082                 shexinfo.lpIDList = get_to_absolute_pidl(entry, hwnd);
3083
3084                 if (!ShellExecuteEx(&shexinfo)) {
3085                         display_error(hwnd, GetLastError());
3086                         ret = FALSE;
3087                 }
3088
3089                 if (shexinfo.lpIDList != entry->pidl)
3090                         (*Globals.iMalloc->lpVtbl->Free)(Globals.iMalloc, shexinfo.lpIDList);
3091
3092                 return ret;
3093         }
3094 #endif
3095
3096         get_path(entry, cmd);
3097
3098          /* start program, open document... */
3099         return launch_file(hwnd, cmd, nCmdShow);
3100 }
3101
3102
3103 static void activate_entry(ChildWnd* child, Pane* pane, HWND hwnd)
3104 {
3105         Entry* entry = pane->cur;
3106
3107         if (!entry)
3108                 return;
3109
3110         if (entry->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
3111                 int scanned_old = entry->scanned;
3112
3113                 if (!scanned_old)
3114                         scan_entry(child, entry, hwnd);
3115
3116 #ifndef _NO_EXTENSIONS
3117                 if (entry->data.cFileName[0]=='.' && entry->data.cFileName[1]=='\0')
3118                         return;
3119 #endif
3120
3121                 if (entry->data.cFileName[0]=='.' && entry->data.cFileName[1]=='.' && entry->data.cFileName[2]=='\0') {
3122                         entry = child->left.cur->up;
3123                         collapse_entry(&child->left, entry);
3124                         goto focus_entry;
3125                 } else if (entry->expanded)
3126                         collapse_entry(pane, child->left.cur);
3127                 else {
3128                         expand_entry(child, child->left.cur);
3129
3130                         if (!pane->treePane) focus_entry: {
3131                                 int idx = ListBox_FindItemData(child->left.hwnd, ListBox_GetCurSel(child->left.hwnd), entry);
3132                                 ListBox_SetCurSel(child->left.hwnd, idx);
3133                                 set_curdir(child, entry, hwnd);
3134                         }
3135                 }
3136
3137                 if (!scanned_old) {
3138                         calc_widths(pane, FALSE);
3139
3140 #ifndef _NO_EXTENSIONS
3141                         set_header(pane);
3142 #endif
3143                 }
3144         } else {
3145                 launch_entry(entry, child->hwnd, SW_SHOWNORMAL);
3146         }
3147 }
3148
3149
3150 static BOOL pane_command(Pane* pane, UINT cmd)
3151 {
3152         switch(cmd) {
3153                 case ID_VIEW_NAME:
3154                         if (pane->visible_cols) {
3155                                 pane->visible_cols = 0;
3156                                 calc_widths(pane, TRUE);
3157 #ifndef _NO_EXTENSIONS
3158                                 set_header(pane);
3159 #endif
3160                                 InvalidateRect(pane->hwnd, 0, TRUE);
3161                                 CheckMenuItem(Globals.hMenuView, ID_VIEW_NAME, MF_BYCOMMAND|MF_CHECKED);
3162                                 CheckMenuItem(Globals.hMenuView, ID_VIEW_ALL_ATTRIBUTES, MF_BYCOMMAND);
3163                                 CheckMenuItem(Globals.hMenuView, ID_VIEW_SELECTED_ATTRIBUTES, MF_BYCOMMAND);
3164                         }
3165                         break;
3166
3167                 case ID_VIEW_ALL_ATTRIBUTES:
3168                         if (pane->visible_cols != COL_ALL) {
3169                                 pane->visible_cols = COL_ALL;
3170                                 calc_widths(pane, TRUE);
3171 #ifndef _NO_EXTENSIONS
3172                                 set_header(pane);
3173 #endif
3174                                 InvalidateRect(pane->hwnd, 0, TRUE);
3175                                 CheckMenuItem(Globals.hMenuView, ID_VIEW_NAME, MF_BYCOMMAND);
3176                                 CheckMenuItem(Globals.hMenuView, ID_VIEW_ALL_ATTRIBUTES, MF_BYCOMMAND|MF_CHECKED);
3177                                 CheckMenuItem(Globals.hMenuView, ID_VIEW_SELECTED_ATTRIBUTES, MF_BYCOMMAND);
3178                         }
3179                         break;
3180
3181 #ifndef _NO_EXTENSIONS
3182                 case ID_PREFERED_SIZES: {
3183                         calc_widths(pane, TRUE);
3184                         set_header(pane);
3185                         InvalidateRect(pane->hwnd, 0, TRUE);
3186                         break;}
3187 #endif
3188
3189                         /* TODO: more command ids... */
3190
3191                 default:
3192                         return FALSE;
3193         }
3194
3195         return TRUE;
3196 }
3197
3198
3199 static HRESULT ShellFolderContextMenu(IShellFolder* shell_folder, HWND hwndParent, int cidl, LPCITEMIDLIST* apidl, int x, int y)
3200 {
3201         IContextMenu* pcm;
3202
3203         HRESULT hr = (*shell_folder->lpVtbl->GetUIObjectOf)(shell_folder, hwndParent, cidl, apidl, &IID_IContextMenu, NULL, (LPVOID*)&pcm);
3204 /*      HRESULT hr = CDefFolderMenu_Create2(dir?dir->_pidl:DesktopFolder(), hwndParent, 1, &pidl, shell_folder, NULL, 0, NULL, &pcm); */
3205
3206         if (SUCCEEDED(hr)) {
3207                 HMENU hmenu = CreatePopupMenu();
3208
3209                 if (hmenu) {
3210                         hr = (*pcm->lpVtbl->QueryContextMenu)(pcm, hmenu, 0, FCIDM_SHVIEWFIRST, FCIDM_SHVIEWLAST, CMF_NORMAL);
3211
3212                         if (SUCCEEDED(hr)) {
3213                                 UINT idCmd = TrackPopupMenu(hmenu, TPM_LEFTALIGN|TPM_RETURNCMD|TPM_RIGHTBUTTON, x, y, 0, hwndParent, NULL);
3214
3215                                 if (idCmd) {
3216                                   CMINVOKECOMMANDINFO cmi;
3217
3218                                   cmi.cbSize = sizeof(CMINVOKECOMMANDINFO);
3219                                   cmi.fMask = 0;
3220                                   cmi.hwnd = hwndParent;
3221                                   cmi.lpVerb = (LPCSTR)(INT_PTR)(idCmd - FCIDM_SHVIEWFIRST);
3222                                   cmi.lpParameters = NULL;
3223                                   cmi.lpDirectory = NULL;
3224                                   cmi.nShow = SW_SHOWNORMAL;
3225                                   cmi.dwHotKey = 0;
3226                                   cmi.hIcon = 0;
3227
3228                                   hr = (*pcm->lpVtbl->InvokeCommand)(pcm, &cmi);
3229                                 }
3230                         }
3231                 }
3232
3233                 (*pcm->lpVtbl->Release)(pcm);
3234         }
3235
3236         return hr;
3237 }
3238
3239
3240 LRESULT CALLBACK ChildWndProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam)
3241 {
3242         static int last_split;
3243
3244         ChildWnd* child = (ChildWnd*) GetWindowLong(hwnd, GWL_USERDATA);
3245         ASSERT(child);
3246
3247         switch(nmsg) {
3248                 case WM_DRAWITEM: {
3249                         LPDRAWITEMSTRUCT dis = (LPDRAWITEMSTRUCT)lparam;
3250                         Entry* entry = (Entry*) dis->itemData;
3251
3252                         if (dis->CtlID == IDW_TREE_LEFT)
3253                                 draw_item(&child->left, dis, entry, -1);
3254                         else
3255                                 draw_item(&child->right, dis, entry, -1);
3256
3257                         return TRUE;}
3258
3259                 case WM_CREATE:
3260                         InitChildWindow(child);
3261                         break;
3262
3263                 case WM_NCDESTROY:
3264                         free_child_window(child);
3265                         SetWindowLong(hwnd, GWL_USERDATA, 0);
3266                         break;
3267
3268                 case WM_PAINT: {
3269                         PAINTSTRUCT ps;
3270                         HBRUSH lastBrush;
3271                         RECT rt;
3272                         GetClientRect(hwnd, &rt);
3273                         BeginPaint(hwnd, &ps);
3274                         rt.left = child->split_pos-SPLIT_WIDTH/2;
3275                         rt.right = child->split_pos+SPLIT_WIDTH/2+1;
3276                         lastBrush = SelectBrush(ps.hdc, (HBRUSH)GetStockObject(COLOR_SPLITBAR));
3277                         Rectangle(ps.hdc, rt.left, rt.top-1, rt.right, rt.bottom+1);
3278                         SelectObject(ps.hdc, lastBrush);
3279 #ifdef _NO_EXTENSIONS
3280                         rt.top = rt.bottom - GetSystemMetrics(SM_CYHSCROLL);
3281                         FillRect(ps.hdc, &rt, GetStockObject(BLACK_BRUSH));
3282 #endif
3283                         EndPaint(hwnd, &ps);
3284                         break;}
3285
3286                 case WM_SETCURSOR:
3287                         if (LOWORD(lparam) == HTCLIENT) {
3288                                 POINT pt;
3289                                 GetCursorPos(&pt);
3290                                 ScreenToClient(hwnd, &pt);
3291
3292                                 if (pt.x>=child->split_pos-SPLIT_WIDTH/2 && pt.x<child->split_pos+SPLIT_WIDTH/2+1) {
3293                                         SetCursor(LoadCursor(0, IDC_SIZEWE));
3294                                         return TRUE;
3295                                 }
3296                         }
3297                         goto def;
3298
3299                 case WM_LBUTTONDOWN: {
3300                         RECT rt;
3301                         int x = LOWORD(lparam);
3302
3303                         GetClientRect(hwnd, &rt);
3304
3305                         if (x>=child->split_pos-SPLIT_WIDTH/2 && x<child->split_pos+SPLIT_WIDTH/2+1) {
3306                                 last_split = child->split_pos;
3307 #ifdef _NO_EXTENSIONS
3308                                 draw_splitbar(hwnd, last_split);
3309 #endif
3310                                 SetCapture(hwnd);
3311                         }
3312
3313                         break;}
3314
3315                 case WM_LBUTTONUP:
3316                         if (GetCapture() == hwnd) {
3317 #ifdef _NO_EXTENSIONS
3318                                 RECT rt;
3319                                 int x = LOWORD(lparam);
3320                                 draw_splitbar(hwnd, last_split);
3321                                 last_split = -1;
3322                                 GetClientRect(hwnd, &rt);
3323                                 child->split_pos = x;
3324                                 resize_tree(child, rt.right, rt.bottom);
3325 #endif
3326                                 ReleaseCapture();
3327                         }
3328                         break;
3329
3330 #ifdef _NO_EXTENSIONS
3331                 case WM_CAPTURECHANGED:
3332                         if (GetCapture()==hwnd && last_split>=0)
3333                                 draw_splitbar(hwnd, last_split);
3334                         break;
3335 #endif
3336
3337                 case WM_KEYDOWN:
3338                         if (wparam == VK_ESCAPE)
3339                                 if (GetCapture() == hwnd) {
3340                                         RECT rt;
3341 #ifdef _NO_EXTENSIONS
3342                                         draw_splitbar(hwnd, last_split);
3343 #else
3344                                         child->split_pos = last_split;
3345 #endif
3346                                         GetClientRect(hwnd, &rt);
3347                                         resize_tree(child, rt.right, rt.bottom);
3348                                         last_split = -1;
3349                                         ReleaseCapture();
3350                                         SetCursor(LoadCursor(0, IDC_ARROW));
3351                                 }
3352                         break;
3353
3354                 case WM_MOUSEMOVE:
3355                         if (GetCapture() == hwnd) {
3356                                 RECT rt;
3357                                 int x = LOWORD(lparam);
3358
3359 #ifdef _NO_EXTENSIONS
3360                                 HDC hdc = GetDC(hwnd);
3361                                 GetClientRect(hwnd, &rt);
3362
3363                                 rt.left = last_split-SPLIT_WIDTH/2;
3364                                 rt.right = last_split+SPLIT_WIDTH/2+1;
3365                                 InvertRect(hdc, &rt);
3366
3367                                 last_split = x;
3368                                 rt.left = x-SPLIT_WIDTH/2;
3369                                 rt.right = x+SPLIT_WIDTH/2+1;
3370                                 InvertRect(hdc, &rt);
3371
3372                                 ReleaseDC(hwnd, hdc);
3373 #else
3374                                 GetClientRect(hwnd, &rt);
3375
3376                                 if (x>=0 && x<rt.right) {
3377                                         child->split_pos = x;
3378                                         resize_tree(child, rt.right, rt.bottom);
3379                                         rt.left = x-SPLIT_WIDTH/2;
3380                                         rt.right = x+SPLIT_WIDTH/2+1;
3381                                         InvalidateRect(hwnd, &rt, FALSE);
3382                                         UpdateWindow(child->left.hwnd);
3383                                         UpdateWindow(hwnd);
3384                                         UpdateWindow(child->right.hwnd);
3385                                 }
3386 #endif
3387                         }
3388                         break;
3389
3390 #ifndef _NO_EXTENSIONS
3391                 case WM_GETMINMAXINFO:
3392                         DefMDIChildProc(hwnd, nmsg, wparam, lparam);
3393
3394                         {LPMINMAXINFO lpmmi = (LPMINMAXINFO)lparam;
3395
3396                         lpmmi->ptMaxTrackSize.x <<= 1;/*2*GetSystemMetrics(SM_CXSCREEN) / SM_CXVIRTUALSCREEN */
3397                         lpmmi->ptMaxTrackSize.y <<= 1;/*2*GetSystemMetrics(SM_CYSCREEN) / SM_CYVIRTUALSCREEN */
3398                         break;}
3399 #endif /* _NO_EXTENSIONS */
3400
3401                 case WM_SETFOCUS:
3402                         SetCurrentDirectory(child->path);
3403                         SetFocus(child->focus_pane? child->right.hwnd: child->left.hwnd);
3404                         break;
3405
3406                 case WM_DISPATCH_COMMAND: {
3407                         Pane* pane = GetFocus()==child->left.hwnd? &child->left: &child->right;
3408
3409                         switch(LOWORD(wparam)) {
3410                                 case ID_WINDOW_NEW: {
3411                                         ChildWnd* new_child = alloc_child_window(child->path, NULL, hwnd);
3412
3413                                         if (!create_child_window(new_child))
3414                                                 free(new_child);
3415
3416                                         break;}
3417
3418                                 case ID_REFRESH:
3419                                         scan_entry(child, pane->cur, hwnd);
3420                                         break;
3421
3422                                 case ID_ACTIVATE:
3423                                         activate_entry(child, pane, hwnd);
3424                                         break;
3425
3426                                 default:
3427                                         return pane_command(pane, LOWORD(wparam));
3428                         }
3429
3430                         return TRUE;}
3431
3432                 case WM_COMMAND: {
3433                         Pane* pane = GetFocus()==child->left.hwnd? &child->left: &child->right;
3434
3435                         switch(HIWORD(wparam)) {
3436                                 case LBN_SELCHANGE: {
3437                                         int idx = ListBox_GetCurSel(pane->hwnd);
3438                                         Entry* entry = (Entry*) ListBox_GetItemData(pane->hwnd, idx);
3439
3440                                         if (pane == &child->left)
3441                                                 set_curdir(child, entry, hwnd);
3442                                         else
3443                                                 pane->cur = entry;
3444                                         break;}
3445
3446                                 case LBN_DBLCLK:
3447                                         activate_entry(child, pane, hwnd);
3448                                         break;
3449                         }
3450                         break;}
3451
3452 #ifndef _NO_EXTENSIONS
3453                 case WM_NOTIFY: {
3454                         NMHDR* pnmh = (NMHDR*) lparam;
3455                         return pane_notify(pnmh->idFrom==IDW_HEADER_LEFT? &child->left: &child->right, pnmh);}
3456 #endif
3457
3458 #ifdef _SHELL_FOLDERS
3459                 case WM_CONTEXTMENU: {
3460                         Pane* pane;
3461                         int idx;
3462
3463                          /* first select the current item in the listbox */
3464                         HWND hpanel = (HWND) wparam;
3465                         POINTS* ppos = &MAKEPOINTS(lparam);
3466                         POINT pt; POINTSTOPOINT(pt, *ppos);
3467                         ScreenToClient(hpanel, &pt);
3468                         SendMessage(hpanel, WM_LBUTTONDOWN, 0, MAKELONG(pt.x, pt.y));
3469                         SendMessage(hpanel, WM_LBUTTONUP, 0, MAKELONG(pt.x, pt.y));
3470
3471                          /* now create the popup menu using shell namespace and IContextMenu */
3472                         pane = GetFocus()==child->left.hwnd? &child->left: &child->right;
3473                         idx = ListBox_GetCurSel(pane->hwnd);
3474
3475                         if (idx != -1) {
3476                                 HRESULT hr;
3477                                 Entry* entry = (Entry*) ListBox_GetItemData(pane->hwnd, idx);
3478
3479                                 LPITEMIDLIST pidl_abs = get_to_absolute_pidl(entry, hwnd);
3480
3481                                 if (pidl_abs) {
3482                                         IShellFolder* parentFolder;
3483                                         LPCITEMIDLIST pidlLast;
3484
3485                                          /* get and use the parent folder to display correct context menu in all cases */
3486                                         if (SUCCEEDED(SHBindToParent(pidl_abs, &IID_IShellFolder, (LPVOID*)&parentFolder, &pidlLast))) {
3487                                                 hr = ShellFolderContextMenu(parentFolder, hwnd, 1, &pidlLast, ppos->x, ppos->y);
3488
3489                                                 (*parentFolder->lpVtbl->Release)(parentFolder);
3490                                         }
3491
3492                                         (*Globals.iMalloc->lpVtbl->Free)(Globals.iMalloc, pidl_abs);
3493                                 }
3494                         }
3495                         break;}
3496 #endif
3497
3498                 case WM_SIZE:
3499                         if (wparam != SIZE_MINIMIZED)
3500                                 resize_tree(child, LOWORD(lparam), HIWORD(lparam));
3501                         /* fall through */
3502
3503                 default: def:
3504                         return DefMDIChildProc(hwnd, nmsg, wparam, lparam);
3505         }
3506
3507         return 0;
3508 }
3509
3510
3511 LRESULT CALLBACK TreeWndProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam)
3512 {
3513         ChildWnd* child = (ChildWnd*) GetWindowLong(GetParent(hwnd), GWL_USERDATA);
3514         Pane* pane = (Pane*) GetWindowLong(hwnd, GWL_USERDATA);
3515         ASSERT(child);
3516
3517         switch(nmsg) {
3518 #ifndef _NO_EXTENSIONS
3519                 case WM_HSCROLL:
3520                         set_header(pane);
3521                         break;
3522 #endif
3523
3524                 case WM_SETFOCUS:
3525                         child->focus_pane = pane==&child->right? 1: 0;
3526                         ListBox_SetSel(hwnd, TRUE, 1);
3527                         /*TODO: check menu items */
3528                         break;
3529
3530                 case WM_KEYDOWN:
3531                         if (wparam == VK_TAB) {
3532                                 /*TODO: SetFocus(Globals.hdrivebar) */
3533                                 SetFocus(child->focus_pane? child->left.hwnd: child->right.hwnd);
3534                         }
3535         }
3536
3537         return CallWindowProc(g_orgTreeWndProc, hwnd, nmsg, wparam, lparam);
3538 }
3539
3540
3541 static void InitInstance(HINSTANCE hinstance)
3542 {
3543         WNDCLASSEX wcFrame;
3544         WNDCLASS wcChild;
3545         ATOM hChildClass;
3546
3547         INITCOMMONCONTROLSEX icc = {
3548                 sizeof(INITCOMMONCONTROLSEX),
3549                 ICC_BAR_CLASSES
3550         };
3551
3552         HDC hdc = GetDC(0);
3553
3554         setlocale(LC_COLLATE, "");      /* set collating rules to local settings for compareName */
3555
3556         InitCommonControlsEx(&icc);
3557
3558
3559         /* register frame window class */
3560
3561         wcFrame.cbSize        = sizeof(WNDCLASSEX);
3562         wcFrame.style         = 0;
3563         wcFrame.lpfnWndProc   = FrameWndProc;
3564         wcFrame.cbClsExtra    = 0;
3565         wcFrame.cbWndExtra    = 0;
3566         wcFrame.hInstance     = hinstance;
3567         wcFrame.hIcon         = LoadIcon(hinstance, MAKEINTRESOURCE(IDI_WINEFILE));
3568         wcFrame.hCursor       = LoadCursor(0, IDC_ARROW);
3569         wcFrame.hbrBackground = 0;
3570         wcFrame.lpszMenuName  = 0;
3571         wcFrame.lpszClassName = WINEFILEFRAME;
3572         wcFrame.hIconSm       = (HICON)LoadImage(hinstance,
3573                                                                                          MAKEINTRESOURCE(IDI_WINEFILE),
3574                                                                                          IMAGE_ICON,
3575                                                                                          GetSystemMetrics(SM_CXSMICON),
3576                                                                                          GetSystemMetrics(SM_CYSMICON),
3577                                                                                          LR_SHARED);
3578
3579         Globals.hframeClass = RegisterClassEx(&wcFrame);
3580
3581
3582         /* register tree windows class */
3583
3584         wcChild.style         = CS_CLASSDC|CS_DBLCLKS|CS_VREDRAW;
3585         wcChild.lpfnWndProc   = ChildWndProc;
3586         wcChild.cbClsExtra    = 0;
3587         wcChild.cbWndExtra    = 0;
3588         wcChild.hInstance     = hinstance;
3589         wcChild.hIcon         = 0;
3590         wcChild.hCursor       = LoadCursor(0, IDC_ARROW);
3591         wcChild.hbrBackground = 0;
3592         wcChild.lpszMenuName  = 0;
3593         wcChild.lpszClassName = WINEFILETREE;
3594
3595         hChildClass = RegisterClass(&wcChild);
3596
3597
3598         Globals.haccel = LoadAccelerators(hinstance, MAKEINTRESOURCE(IDA_WINEFILE));
3599
3600         Globals.hfont = CreateFont(-MulDiv(8,GetDeviceCaps(hdc,LOGPIXELSY),72), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, TEXT("MS Shell Dlg"));
3601
3602         ReleaseDC(0, hdc);
3603
3604         Globals.hInstance = hinstance;
3605
3606 #ifdef _SHELL_FOLDERS
3607         CoInitialize(NULL);
3608         CoGetMalloc(MEMCTX_TASK, &Globals.iMalloc);
3609         SHGetDesktopFolder(&Globals.iDesktop);
3610         Globals.cfStrFName = RegisterClipboardFormat(CFSTR_FILENAME);
3611 #endif
3612 }
3613
3614
3615 void show_frame(HWND hwndParent, int cmdshow)
3616 {
3617         TCHAR path[MAX_PATH];
3618         ChildWnd* child;
3619         HMENU hMenuFrame, hMenuWindow;
3620
3621         CLIENTCREATESTRUCT ccs;
3622
3623         if (Globals.hMainWnd)
3624                 return;
3625
3626         Globals.hwndParent = hwndParent;
3627
3628         hMenuFrame = LoadMenu(Globals.hInstance, MAKEINTRESOURCE(IDM_WINEFILE));
3629         hMenuWindow = GetSubMenu(hMenuFrame, GetMenuItemCount(hMenuFrame)-2);
3630
3631         Globals.hMenuFrame = hMenuFrame;
3632         Globals.hMenuView = GetSubMenu(hMenuFrame, 3);
3633         Globals.hMenuOptions = GetSubMenu(hMenuFrame, 4);
3634
3635         ccs.hWindowMenu  = hMenuWindow;
3636         ccs.idFirstChild = IDW_FIRST_CHILD;
3637
3638
3639         /* create main window */
3640         Globals.hMainWnd = CreateWindowEx(0, (LPCTSTR)(int)Globals.hframeClass, TEXT("Wine File"), WS_OVERLAPPEDWINDOW,
3641                                         CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
3642                                         hwndParent, Globals.hMenuFrame, Globals.hInstance, 0/*lpParam*/);
3643
3644
3645         Globals.hmdiclient = CreateWindowEx(0, TEXT("MDICLIENT"), NULL,
3646                                         WS_CHILD|WS_CLIPCHILDREN|WS_VSCROLL|WS_HSCROLL|WS_VISIBLE|WS_BORDER,
3647                                         0, 0, 0, 0,
3648                                         Globals.hMainWnd, 0, Globals.hInstance, &ccs);
3649
3650
3651         {
3652                 TBBUTTON drivebarBtn = {0, 0, TBSTATE_ENABLED, BTNS_SEP, {0, 0}, 0, 0};
3653                 int btn = 1;
3654                 PTSTR p;
3655
3656                 Globals.hdrivebar = CreateToolbarEx(Globals.hMainWnd, WS_CHILD|WS_VISIBLE|CCS_NOMOVEY|TBSTYLE_LIST,
3657                                         IDW_DRIVEBAR, 2, Globals.hInstance, IDB_DRIVEBAR, &drivebarBtn,
3658                                         1, 16, 13, 16, 13, sizeof(TBBUTTON));
3659                 CheckMenuItem(Globals.hMenuOptions, ID_VIEW_DRIVE_BAR, MF_BYCOMMAND|MF_CHECKED);
3660
3661                 GetLogicalDriveStrings(BUFFER_LEN, Globals.drives);
3662
3663                 drivebarBtn.fsStyle = BTNS_BUTTON;
3664
3665 #ifndef _NO_EXTENSIONS
3666 #ifdef __WINE__
3667                 /* insert unix file system button */
3668                 SendMessage(Globals.hdrivebar, TB_ADDSTRING, 0, (LPARAM)TEXT("/\0"));
3669
3670                 drivebarBtn.idCommand = ID_DRIVE_UNIX_FS;
3671                 SendMessage(Globals.hdrivebar, TB_INSERTBUTTON, btn++, (LPARAM)&drivebarBtn);
3672                 drivebarBtn.iString++;
3673 #endif
3674 #ifdef _SHELL_FOLDERS
3675                 /* insert shell namespace button */
3676                 SendMessage(Globals.hdrivebar, TB_ADDSTRING, 0, (LPARAM)TEXT("Shell\0"));
3677
3678                 drivebarBtn.idCommand = ID_DRIVE_SHELL_NS;
3679                 SendMessage(Globals.hdrivebar, TB_INSERTBUTTON, btn++, (LPARAM)&drivebarBtn);
3680                 drivebarBtn.iString++;
3681 #endif
3682
3683                 /* register windows drive root strings */
3684                 SendMessage(Globals.hdrivebar, TB_ADDSTRING, 0, (LPARAM)Globals.drives);
3685 #endif
3686
3687                 drivebarBtn.idCommand = ID_DRIVE_FIRST;
3688
3689                 for(p=Globals.drives; *p; ) {
3690 #ifdef _NO_EXTENSIONS
3691                   /* insert drive letter */
3692                         TCHAR b[3] = {tolower(*p)};
3693                         SendMessage(Globals.hdrivebar, TB_ADDSTRING, 0, (LPARAM)b);
3694 #endif
3695                         switch(GetDriveType(p)) {
3696                                 case DRIVE_REMOVABLE:   drivebarBtn.iBitmap = 1;        break;
3697                                 case DRIVE_CDROM:               drivebarBtn.iBitmap = 3;        break;
3698                                 case DRIVE_REMOTE:              drivebarBtn.iBitmap = 4;        break;
3699                                 case DRIVE_RAMDISK:             drivebarBtn.iBitmap = 5;        break;
3700                                 default:/*DRIVE_FIXED*/ drivebarBtn.iBitmap = 2;
3701                         }
3702
3703                         SendMessage(Globals.hdrivebar, TB_INSERTBUTTON, btn++, (LPARAM)&drivebarBtn);
3704                         drivebarBtn.idCommand++;
3705                         drivebarBtn.iString++;
3706
3707                         while(*p++);
3708                 }
3709         }
3710
3711         {
3712                 TBBUTTON toolbarBtns[] = {
3713                         {0, 0, 0, BTNS_SEP, {0, 0}, 0, 0},
3714                         {0, ID_WINDOW_NEW, TBSTATE_ENABLED, BTNS_BUTTON, {0, 0}, 0, 0},
3715                         {1, ID_WINDOW_CASCADE, TBSTATE_ENABLED, BTNS_BUTTON, {0, 0}, 0, 0},
3716                         {2, ID_WINDOW_TILE_HORZ, TBSTATE_ENABLED, BTNS_BUTTON, {0, 0}, 0, 0},
3717                         {3, ID_WINDOW_TILE_VERT, TBSTATE_ENABLED, BTNS_BUTTON, {0, 0}, 0, 0},
3718 /*TODO
3719                         {4, ID_... , TBSTATE_ENABLED, BTNS_BUTTON},
3720                         {5, ID_... , TBSTATE_ENABLED, BTNS_BUTTON},
3721 */              };
3722
3723                 Globals.htoolbar = CreateToolbarEx(Globals.hMainWnd, WS_CHILD|WS_VISIBLE,
3724                         IDW_TOOLBAR, 2, Globals.hInstance, IDB_TOOLBAR, toolbarBtns,
3725                         sizeof(toolbarBtns)/sizeof(TBBUTTON), 16, 15, 16, 15, sizeof(TBBUTTON));
3726                 CheckMenuItem(Globals.hMenuOptions, ID_VIEW_TOOL_BAR, MF_BYCOMMAND|MF_CHECKED);
3727         }
3728
3729         Globals.hstatusbar = CreateStatusWindow(WS_CHILD|WS_VISIBLE, 0, Globals.hMainWnd, IDW_STATUSBAR);
3730         CheckMenuItem(Globals.hMenuOptions, ID_VIEW_STATUSBAR, MF_BYCOMMAND|MF_CHECKED);
3731
3732 /* CreateStatusWindow does not accept WS_BORDER
3733         Globals.hstatusbar = CreateWindowEx(WS_EX_NOPARENTNOTIFY, STATUSCLASSNAME, 0,
3734                                         WS_CHILD|WS_VISIBLE|WS_CLIPSIBLINGS|WS_BORDER|CCS_NODIVIDER, 0,0,0,0,
3735                                         Globals.hMainWnd, (HMENU)IDW_STATUSBAR, hinstance, 0);*/
3736
3737         /*TODO: read paths and window placements from registry */
3738         GetCurrentDirectory(MAX_PATH, path);
3739
3740         ShowWindow(Globals.hMainWnd, cmdshow);
3741
3742 #if defined(_SHELL_FOLDERS) && !defined(__WINE__)
3743          /* Shell Namespace as default: */
3744         child = alloc_child_window(path, get_path_pidl(path,Globals.hMainWnd), Globals.hMainWnd);
3745 #else
3746         child = alloc_child_window(path, NULL, Globals.hMainWnd);
3747 #endif
3748
3749         child->pos.showCmd = SW_SHOWMAXIMIZED;
3750         child->pos.rcNormalPosition.left = 0;
3751         child->pos.rcNormalPosition.top = 0;
3752         child->pos.rcNormalPosition.right = 320;
3753         child->pos.rcNormalPosition.bottom = 280;
3754
3755         if (!create_child_window(child))
3756                 free(child);
3757
3758         SetWindowPlacement(child->hwnd, &child->pos);
3759
3760         Globals.himl = ImageList_LoadBitmap(Globals.hInstance, MAKEINTRESOURCE(IDB_IMAGES), 16, 0, RGB(0,255,0));
3761
3762         Globals.prescan_node = FALSE;
3763
3764         UpdateWindow(Globals.hMainWnd);
3765 }
3766
3767 void ExitInstance()
3768 {
3769 #ifdef _SHELL_FOLDERS
3770         (*Globals.iDesktop->lpVtbl->Release)(Globals.iDesktop);
3771         (*Globals.iMalloc->lpVtbl->Release)(Globals.iMalloc);
3772         CoUninitialize();
3773 #endif
3774
3775         ImageList_Destroy(Globals.himl);
3776 }
3777
3778
3779 /* search for already running win[e]files */
3780
3781 static int g_foundPrevInstance = 0;
3782
3783 static BOOL CALLBACK EnumWndProc(HWND hwnd, LPARAM lparam)
3784 {
3785         TCHAR cls[128];
3786
3787         GetClassName(hwnd, cls, 128);
3788
3789         if (!lstrcmp(cls, (LPCTSTR)lparam)) {
3790                 g_foundPrevInstance++;
3791                 return FALSE;
3792         }
3793
3794         return TRUE;
3795 }
3796
3797 /* search for window of given class name to allow only one running instance */
3798 int find_window_class(LPCTSTR classname)
3799 {
3800         EnumWindows(EnumWndProc, (LPARAM)classname);
3801
3802         if (g_foundPrevInstance)
3803                 return 1;
3804
3805         return 0;
3806 }
3807
3808
3809 int winefile_main(HINSTANCE hinstance, HWND hwndParent, int cmdshow)
3810 {
3811         MSG msg;
3812
3813         InitInstance(hinstance);
3814
3815 #ifndef _ROS_   /* don't maximize if being called from the ROS desktop */
3816         if (cmdshow == SW_SHOWNORMAL)
3817                 /*TODO: read window placement from registry */
3818                 cmdshow = SW_MAXIMIZE;
3819 #endif
3820
3821         show_frame(hwndParent, cmdshow);
3822
3823         while(GetMessage(&msg, 0, 0, 0)) {
3824                 if (Globals.hmdiclient && TranslateMDISysAccel(Globals.hmdiclient, &msg))
3825                         continue;
3826
3827                 if (Globals.hMainWnd && TranslateAccelerator(Globals.hMainWnd, Globals.haccel, &msg))
3828                         continue;
3829
3830                 TranslateMessage(&msg);
3831                 DispatchMessage(&msg);
3832         }
3833
3834         ExitInstance();
3835
3836         return msg.wParam;
3837 }
3838
3839
3840 #ifndef _ROS_
3841
3842 int APIENTRY WinMain(HINSTANCE hinstance,
3843                                          HINSTANCE previnstance,
3844                                          LPSTR     cmdline,
3845                                          int       cmdshow)
3846 {
3847 #ifdef _NO_EXTENSIONS
3848         if (find_window_class(WINEFILEFRAME))
3849                 return 1;
3850 #endif
3851
3852         winefile_main(hinstance, 0, cmdshow);
3853
3854         return 0;
3855 }
3856
3857 #endif