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