Fixed some issues found by winapi_check.
[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 #include "config.h"
22 #include "wine/port.h"
23
24 #include "winefile.h"
25 #include "resource.h"
26
27
28 /* for read_directory_unix() */
29 #if !defined(_NO_EXTENSIONS)
30 #include <dirent.h>
31 #include <sys/stat.h>
32 #include <unistd.h>
33 #include <time.h>
34 #endif
35
36 #ifdef _NO_EXTENSIONS
37 #undef _LEFT_FILES
38 #endif
39
40 #ifndef _MAX_PATH
41 #define _MAX_DRIVE          3
42 #define _MAX_FNAME          256
43 #define _MAX_DIR            _MAX_FNAME
44 #define _MAX_EXT            _MAX_FNAME
45 #define _MAX_PATH           260
46 #endif
47
48 WINEFILE_GLOBALS Globals;
49
50 extern void WineLicense(HWND hWnd);
51 extern void WineWarranty(HWND hWnd);
52
53 typedef struct _Entry {
54         struct _Entry*  next;
55         struct _Entry*  down;
56         struct _Entry*  up;
57
58         BOOL    expanded;
59         BOOL    scanned;
60         int             level;
61
62         WIN32_FIND_DATA data;
63
64 #ifndef _NO_EXTENSIONS
65         BY_HANDLE_FILE_INFORMATION bhfi;
66         BOOL    bhfi_valid;
67         BOOL    unix_dir;
68 #endif
69 } Entry;
70
71 typedef struct {
72         Entry   entry;
73         TCHAR   path[MAX_PATH];
74         TCHAR   volname[_MAX_FNAME];
75         TCHAR   fs[_MAX_DIR];
76         DWORD   drive_type;
77         DWORD   fs_flags;
78 } Root;
79
80 enum COLUMN_FLAGS {
81         COL_SIZE                = 0x01,
82         COL_DATE                = 0x02,
83         COL_TIME                = 0x04,
84         COL_ATTRIBUTES  = 0x08,
85         COL_DOSNAMES    = 0x10,
86 #ifdef _NO_EXTENSIONS
87         COL_ALL = COL_SIZE|COL_DATE|COL_TIME|COL_ATTRIBUTES|COL_DOSNAMES
88 #else
89         COL_INDEX               = 0x20,
90         COL_LINKS               = 0x40,
91         COL_ALL = COL_SIZE|COL_DATE|COL_TIME|COL_ATTRIBUTES|COL_DOSNAMES|COL_INDEX|COL_LINKS
92 #endif
93 };
94
95 typedef enum {
96         SORT_NAME,
97         SORT_EXT,
98         SORT_SIZE,
99         SORT_DATE
100 } SORT_ORDER;
101
102 typedef struct {
103         HWND    hwnd;
104 #ifndef _NO_EXTENSIONS
105         HWND    hwndHeader;
106 #endif
107
108 #ifndef _NO_EXTENSIONS
109 #define COLUMNS 10
110 #else
111 #define COLUMNS 5
112 #endif
113         int             widths[COLUMNS];
114         int             positions[COLUMNS+1];
115
116         BOOL    treePane;
117         int             visible_cols;
118         Entry*  root;
119         Entry*  cur;
120 } Pane;
121
122 typedef struct {
123         HWND    hwnd;
124         Pane    left;
125         Pane    right;
126         int             focus_pane;             /* 0: left  1: right */
127         WINDOWPLACEMENT pos;
128         int             split_pos;
129         BOOL    header_wdths_ok;
130
131         TCHAR   path[MAX_PATH];
132         Root    root;
133
134         SORT_ORDER sortOrder;
135 } ChildWnd;
136
137
138 static void read_directory(Entry* parent, LPCTSTR path, int sortOrder);
139 static void set_curdir(ChildWnd* child, Entry* entry);
140
141 LRESULT CALLBACK FrameWndProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam);
142 LRESULT CALLBACK ChildWndProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam);
143 LRESULT CALLBACK TreeWndProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam);
144
145
146 static void display_error(HWND hwnd, DWORD error)
147 {
148         PTSTR msg;
149
150         if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
151                 0, error, MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), (PTSTR)&msg, 0, NULL))
152                 MessageBox(hwnd, msg, _T("Winefile"), MB_OK);
153         else
154                 MessageBox(hwnd, _T("Error"), _T("Winefile"), MB_OK);
155
156         LocalFree(msg);
157 }
158
159
160 static void read_directory_win(Entry* parent, LPCTSTR path)
161 {
162         Entry* entry = (Entry*) malloc(sizeof(Entry));
163         int level = parent->level + 1;
164         Entry* last = 0;
165         HANDLE hFind;
166 #ifndef _NO_EXTENSIONS
167         HANDLE hFile;
168 #endif
169
170         TCHAR buffer[MAX_PATH], *p;
171         for(p=buffer; *path; )
172                 *p++ = *path++;
173
174         lstrcpy(p, _T("\\*"));
175
176         hFind = FindFirstFile(buffer, &entry->data);
177
178         if (hFind != INVALID_HANDLE_VALUE) {
179                 parent->down = entry;
180
181                 do {
182                         entry->down = 0;
183                         entry->up = parent;
184                         entry->expanded = FALSE;
185                         entry->scanned = FALSE;
186                         entry->level = level;
187
188 #ifdef _NO_EXTENSIONS
189                         /* hide directory entry "." */
190                         if (entry->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
191                                 LPCTSTR name = entry->data.cFileName;
192
193                                 if (name[0]=='.' && name[1]=='\0')
194                                         continue;
195                         }
196 #else
197                         entry->unix_dir = FALSE;
198                         entry->bhfi_valid = FALSE;
199
200                         lstrcpy(p+1, entry->data.cFileName);
201
202                         hFile = CreateFile(buffer, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
203                                                                 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0);
204
205                         if (hFile != INVALID_HANDLE_VALUE) {
206                                 if (GetFileInformationByHandle(hFile, &entry->bhfi))
207                                         entry->bhfi_valid = TRUE;
208
209                                 CloseHandle(hFile);
210                         }
211 #endif
212
213                         last = entry;
214
215                         entry = (Entry*) malloc(sizeof(Entry));
216
217                         if (last)
218                                 last->next = entry;
219                 } while(FindNextFile(hFind, &entry->data));
220
221                 last->next = 0;
222
223                 FindClose(hFind);
224         } else
225                 parent->down = 0;
226
227         free(entry);
228
229         parent->scanned = TRUE;
230 }
231
232
233 static Entry* find_entry_win(Entry* parent, LPCTSTR name)
234 {
235         Entry* entry;
236
237         for(entry=parent->down; entry; entry=entry->next) {
238                 LPCTSTR p = name;
239                 LPCTSTR q = entry->data.cFileName;
240
241                 do {
242                         if (!*p || *p==_T('\\') || *p==_T('/'))
243                                 return entry;
244                 } while(tolower(*p++) == tolower(*q++));
245
246                 p = name;
247                 q = entry->data.cAlternateFileName;
248
249                 do {
250                         if (!*p || *p==_T('\\') || *p==_T('/'))
251                                 return entry;
252                 } while(tolower(*p++) == tolower(*q++));
253         }
254
255         return 0;
256 }
257
258
259 static Entry* read_tree_win(Root* root, LPCTSTR path, int sortOrder)
260 {
261         TCHAR buffer[MAX_PATH];
262         Entry* entry = &root->entry;
263         LPCTSTR s = path;
264         PTSTR d = buffer;
265
266 #ifndef _NO_EXTENSIONS
267         entry->unix_dir = FALSE;
268 #endif
269
270         while(entry) {
271                 while(*s && *s!=_T('\\') && *s!=_T('/'))
272                         *d++ = *s++;
273
274                 while(*s==_T('\\') || *s==_T('/'))
275                         s++;
276
277                 *d++ = _T('\\');
278                 *d = _T('\0');
279
280                 read_directory(entry, buffer, sortOrder);
281
282                 if (entry->down)
283                         entry->expanded = TRUE;
284
285                 if (!*s)
286                         break;
287
288                 entry = find_entry_win(entry, s);
289         }
290
291         return entry;
292 }
293
294
295 #if !defined(_NO_EXTENSIONS) && defined(__linux__)
296
297 BOOL to_filetime(const time_t* t, FILETIME* ftime)
298 {
299         struct tm* tm = gmtime(t);
300         SYSTEMTIME stime;
301
302         if (!tm)
303                 return FALSE;
304
305         stime.wYear = tm->tm_year+1900;
306         stime.wMonth = tm->tm_mon+1;
307         /*      stime.wDayOfWeek */
308         stime.wDay = tm->tm_mday;
309         stime.wHour = tm->tm_hour;
310         stime.wMinute = tm->tm_min;
311         stime.wSecond = tm->tm_sec;
312
313         return SystemTimeToFileTime(&stime, ftime);
314 }
315
316 static void read_directory_unix(Entry* parent, LPCTSTR path)
317 {
318         Entry* entry = (Entry*) malloc(sizeof(Entry));
319         int level = parent->level + 1;
320         Entry* last = 0;
321
322         DIR* dir = opendir(path);
323
324         if (dir) {
325                 struct stat st;
326                 struct dirent* ent;
327                 TCHAR buffer[MAX_PATH], *p;
328
329                 for(p=buffer; *path; )
330                         *p++ = *path++;
331
332                 if (p==buffer || p[-1]!='/')
333                         *p++ = '/';
334
335                 parent->down = entry;
336
337                 while((ent=readdir(dir))) {
338                         entry->unix_dir = TRUE;
339                         lstrcpy(entry->data.cFileName, ent->d_name);
340                         entry->data.dwFileAttributes = ent->d_name[0]=='.'? FILE_ATTRIBUTE_HIDDEN: 0;
341
342                         strcpy(p, ent->d_name);
343
344                         if (!stat(buffer, &st)) {
345                                 if (S_ISDIR(st.st_mode))
346                                         entry->data.dwFileAttributes |= FILE_ATTRIBUTE_DIRECTORY;
347
348                                 entry->data.nFileSizeLow = st.st_size & 0xFFFFFFFF;
349                                 entry->data.nFileSizeHigh = st.st_size >> 32;
350
351                                 memset(&entry->data.ftCreationTime, 0, sizeof(FILETIME));
352                                 to_filetime(&st.st_atime, &entry->data.ftLastAccessTime);
353                                 to_filetime(&st.st_mtime, &entry->data.ftLastWriteTime);
354
355                                 entry->bhfi.nFileIndexLow = ent->d_ino;
356                                 entry->bhfi.nFileIndexHigh = 0;
357
358                                 entry->bhfi.nNumberOfLinks = st.st_nlink;
359
360                                 entry->bhfi_valid = TRUE;
361                         } else {
362                                 entry->data.nFileSizeLow = 0;
363                                 entry->data.nFileSizeHigh = 0;
364                                 entry->bhfi_valid = FALSE;
365                         }
366
367                         entry->down = 0;
368                         entry->up = parent;
369                         entry->expanded = FALSE;
370                         entry->scanned = FALSE;
371                         entry->level = level;
372
373                         last = entry;
374
375                         entry = (Entry*) malloc(sizeof(Entry));
376
377                         if (last)
378                                 last->next = entry;
379                 }
380
381                 last->next = 0;
382
383                 closedir(dir);
384         } else
385                 parent->down = 0;
386
387         free(entry);
388
389         parent->scanned = TRUE;
390 }
391
392 static Entry* find_entry_unix(Entry* parent, LPCTSTR name)
393 {
394         Entry* entry;
395
396         for(entry=parent->down; entry; entry=entry->next) {
397                 LPCTSTR p = name;
398                 LPCTSTR q = entry->data.cFileName;
399
400                 do {
401                         if (!*p || *p==_T('/'))
402                                 return entry;
403                 } while(*p++ == *q++);
404         }
405
406         return 0;
407 }
408
409 static Entry* read_tree_unix(Root* root, LPCTSTR path, int sortOrder)
410 {
411         TCHAR buffer[MAX_PATH];
412         Entry* entry = &root->entry;
413         LPCTSTR s = path;
414         PTSTR d = buffer;
415
416         entry->unix_dir = TRUE;
417
418         while(entry) {
419                 while(*s && *s!=_T('/'))
420                         *d++ = *s++;
421
422                 while(*s == _T('/'))
423                         s++;
424
425                 *d++ = _T('/');
426                 *d = _T('\0');
427
428                 read_directory(entry, buffer, sortOrder);
429
430                 if (entry->down)
431                         entry->expanded = TRUE;
432
433                 if (!*s)
434                         break;
435
436                 entry = find_entry_unix(entry, s);
437         }
438
439         return entry;
440 }
441
442 #endif
443
444
445 /* directories first... */
446 static int compareType(const WIN32_FIND_DATA* fd1, const WIN32_FIND_DATA* fd2)
447 {
448         int dir1 = fd1->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
449         int dir2 = fd2->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
450
451         return dir2==dir1? 0: dir2<dir1? -1: 1;
452 }
453
454
455 static int compareName(const void* arg1, const void* arg2)
456 {
457         const WIN32_FIND_DATA* fd1 = &(*(Entry**)arg1)->data;
458         const WIN32_FIND_DATA* fd2 = &(*(Entry**)arg2)->data;
459
460         int cmp = compareType(fd1, fd2);
461         if (cmp)
462                 return cmp;
463
464         return lstrcmpi(fd1->cFileName, fd2->cFileName);
465 }
466
467 static int compareExt(const void* arg1, const void* arg2)
468 {
469         const WIN32_FIND_DATA* fd1 = &(*(Entry**)arg1)->data;
470         const WIN32_FIND_DATA* fd2 = &(*(Entry**)arg2)->data;
471         const TCHAR *name1, *name2, *ext1, *ext2;
472
473         int cmp = compareType(fd1, fd2);
474         if (cmp)
475                 return cmp;
476
477         name1 = fd1->cFileName;
478         name2 = fd2->cFileName;
479
480         ext1 = _tcsrchr(name1, _T('.'));
481         ext2 = _tcsrchr(name2, _T('.'));
482
483         if (ext1)
484                 ext1++;
485         else
486                 ext1 = _T("");
487
488         if (ext2)
489                 ext2++;
490         else
491                 ext2 = _T("");
492
493         cmp = lstrcmpi(ext1, ext2);
494         if (cmp)
495                 return cmp;
496
497         return lstrcmpi(name1, name2);
498 }
499
500 static int compareSize(const void* arg1, const void* arg2)
501 {
502         WIN32_FIND_DATA* fd1 = &(*(Entry**)arg1)->data;
503         WIN32_FIND_DATA* fd2 = &(*(Entry**)arg2)->data;
504
505         int cmp = compareType(fd1, fd2);
506         if (cmp)
507                 return cmp;
508
509         cmp = fd2->nFileSizeHigh - fd1->nFileSizeHigh;
510
511         if (cmp < 0)
512                 return -1;
513         else if (cmp > 0)
514                 return 1;
515
516         cmp = fd2->nFileSizeLow - fd1->nFileSizeLow;
517
518         return cmp<0? -1: cmp>0? 1: 0;
519 }
520
521 static int compareDate(const void* arg1, const void* arg2)
522 {
523         WIN32_FIND_DATA* fd1 = &(*(Entry**)arg1)->data;
524         WIN32_FIND_DATA* fd2 = &(*(Entry**)arg2)->data;
525
526         int cmp = compareType(fd1, fd2);
527         if (cmp)
528                 return cmp;
529
530         return CompareFileTime(&fd2->ftLastWriteTime, &fd1->ftLastWriteTime);
531 }
532
533
534 static int (*sortFunctions[])(const void* arg1, const void* arg2) = {
535         compareName,    /* SORT_NAME */
536         compareExt,             /* SORT_EXT */
537         compareSize,    /* SORT_SIZE */
538         compareDate             /* SORT_DATE */
539 };
540
541
542 static void SortDirectory(Entry* parent, SORT_ORDER sortOrder)
543 {
544         Entry* entry = parent->down;
545         Entry** array, **p;
546         int len;
547
548         len = 0;
549         for(entry=parent->down; entry; entry=entry->next)
550                 len++;
551
552         if (len) {
553                 array = (Entry**) alloca(len*sizeof(Entry*));
554
555                 p = array;
556                 for(entry=parent->down; entry; entry=entry->next)
557                         *p++ = entry;
558
559                 /* call qsort with the appropriate compare function */
560                 qsort(array, len, sizeof(array[0]), sortFunctions[sortOrder]);
561
562                 parent->down = array[0];
563
564                 for(p=array; --len; p++)
565                         p[0]->next = p[1];
566
567                 (*p)->next = 0;
568         }
569 }
570
571
572 static void read_directory(Entry* parent, LPCTSTR path, int sortOrder)
573 {
574         TCHAR buffer[MAX_PATH];
575         Entry* entry;
576         LPCTSTR s;
577         PTSTR d;
578
579 #if !defined(_NO_EXTENSIONS) && defined(__linux__)
580         if (parent->unix_dir)
581         {
582                 read_directory_unix(parent, path);
583
584                 if (Globals.prescan_node) {
585                         s = path;
586                         d = buffer;
587
588                         while(*s)
589                                 *d++ = *s++;
590
591                         *d++ = _T('/');
592
593                         for(entry=parent->down; entry; entry=entry->next)
594                                 if (entry->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
595                                         lstrcpy(d, entry->data.cFileName);
596                                         read_directory_unix(entry, buffer);
597                                         SortDirectory(entry, sortOrder);
598                                 }
599                 }
600         }
601         else
602 #endif
603         {
604                 read_directory_win(parent, path);
605
606                 if (Globals.prescan_node) {
607                         s = path;
608                         d = buffer;
609
610                         while(*s)
611                                 *d++ = *s++;
612
613                         *d++ = _T('\\');
614
615                         for(entry=parent->down; entry; entry=entry->next)
616                                 if (entry->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
617                                         lstrcpy(d, entry->data.cFileName);
618                                         read_directory_win(entry, buffer);
619                                         SortDirectory(entry, sortOrder);
620                                 }
621                 }
622         }
623
624         SortDirectory(parent, sortOrder);
625 }
626
627
628 static ChildWnd* alloc_child_window(LPCTSTR path)
629 {
630         TCHAR drv[_MAX_DRIVE+1], dir[_MAX_DIR], name[_MAX_FNAME], ext[_MAX_EXT];
631         ChildWnd* child = (ChildWnd*) malloc(sizeof(ChildWnd));
632         Root* root = &child->root;
633         Entry* entry;
634
635         memset(child, 0, sizeof(ChildWnd));
636
637         child->left.treePane = TRUE;
638         child->left.visible_cols = 0;
639
640         child->right.treePane = FALSE;
641 #ifndef _NO_EXTENSIONS
642         child->right.visible_cols = COL_SIZE|COL_DATE|COL_TIME|COL_ATTRIBUTES|COL_INDEX|COL_LINKS;
643 #else
644         child->right.visible_cols = COL_SIZE|COL_DATE|COL_TIME|COL_ATTRIBUTES;
645 #endif
646
647         child->pos.length = sizeof(WINDOWPLACEMENT);
648         child->pos.flags = 0;
649         child->pos.showCmd = SW_SHOWNORMAL;
650         child->pos.rcNormalPosition.left = CW_USEDEFAULT;
651         child->pos.rcNormalPosition.top = CW_USEDEFAULT;
652         child->pos.rcNormalPosition.right = CW_USEDEFAULT;
653         child->pos.rcNormalPosition.bottom = CW_USEDEFAULT;
654
655         child->focus_pane = 0;
656         child->split_pos = 200;
657         child->sortOrder = SORT_NAME;
658         child->header_wdths_ok = FALSE;
659
660         lstrcpy(child->path, path);
661
662         _tsplitpath(path, drv, dir, name, ext);
663
664 #if !defined(_NO_EXTENSIONS) && defined(__linux__)
665         if (*path == '/')
666         {
667                 root->drive_type = GetDriveType(path);
668
669                 lstrcat(drv, _T("/"));
670                 lstrcpy(root->volname, _T("root fs"));
671                 root->fs_flags = 0;
672                 lstrcpy(root->fs, _T("unixfs"));
673
674                 lstrcpy(root->path, _T("/"));
675                 entry = read_tree_unix(root, path, child->sortOrder);
676         }
677         else
678 #endif
679         {
680                 root->drive_type = GetDriveType(path);
681
682                 lstrcat(drv, _T("\\"));
683                 GetVolumeInformation(drv, root->volname, _MAX_FNAME, 0, 0, &root->fs_flags, root->fs, _MAX_DIR);
684
685                 lstrcpy(root->path, drv);
686                 entry = read_tree_win(root, path, child->sortOrder);
687         }
688
689         /*@@lstrcpy(root->entry.data.cFileName, drv); */
690         wsprintf(root->entry.data.cFileName, _T("%s - %s"), drv, root->fs);
691
692         root->entry.data.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
693
694         child->left.root = &root->entry;
695
696         set_curdir(child, entry);
697
698         return child;
699 }
700
701
702 /* recursively free all child entries */
703 static void free_entries(Entry* parent)
704 {
705         Entry *entry, *next=parent->down;
706
707         if (next) {
708                 parent->down = 0;
709
710                 do {
711                         entry = next;
712                         next = entry->next;
713
714                         free_entries(entry);
715                         free(entry);
716                 } while(next);
717         }
718 }
719
720 /* free all memory associated with a child window */
721 static void free_child_window(ChildWnd* child)
722 {
723         free_entries(&child->root.entry);
724         free(child);
725 }
726
727
728 /* get full path of specified directory entry */
729 static void get_path(Entry* dir, PTSTR path)
730 {
731         Entry* entry;
732         int len = 0;
733         int level = 0;
734
735         for(entry=dir; entry; level++) {
736                 LPCTSTR name = entry->data.cFileName;
737                 LPCTSTR s = name;
738                 int l;
739
740                 for(l=0; *s && *s!=_T('/') && *s!=_T('\\'); s++)
741                         l++;
742
743                 if (entry->up) {
744                         memmove(path+l+1, path, len*sizeof(TCHAR));
745                         memcpy(path+1, name, l*sizeof(TCHAR));
746                         len += l+1;
747
748 #ifndef _NO_EXTENSIONS
749                         if (entry->unix_dir)
750                                 path[0] = _T('/');
751                         else
752 #endif
753                                 path[0] = _T('\\');
754
755                         entry = entry->up;
756                 } else {
757                         memmove(path+l, path, len*sizeof(TCHAR));
758                         memcpy(path, name, l*sizeof(TCHAR));
759                         len += l;
760                         break;
761                 }
762         }
763
764         if (!level) {
765 #ifndef _NO_EXTENSIONS
766                 if (entry->unix_dir)
767                         path[len++] = _T('/');
768                 else
769 #endif
770                         path[len++] = _T('\\');
771         }
772
773         path[len] = _T('\0');
774 }
775
776
777 static void resize_frame_rect(HWND hwnd, PRECT prect)
778 {
779         int new_top;
780         RECT rt;
781
782         if (IsWindowVisible(Globals.htoolbar)) {
783                 SendMessage(Globals.htoolbar, WM_SIZE, 0, 0);
784                 GetClientRect(Globals.htoolbar, &rt);
785                 prect->top = rt.bottom+3;
786                 prect->bottom -= rt.bottom+3;
787         }
788
789         if (IsWindowVisible(Globals.hdrivebar)) {
790                 SendMessage(Globals.hdrivebar, WM_SIZE, 0, 0);
791                 GetClientRect(Globals.hdrivebar, &rt);
792                 new_top = --prect->top + rt.bottom+3;
793                 MoveWindow(Globals.hdrivebar, 0, prect->top, rt.right, new_top, TRUE);
794                 prect->top = new_top;
795                 prect->bottom -= rt.bottom+2;
796         }
797
798         if (IsWindowVisible(Globals.hstatusbar)) {
799                 int parts[] = {300, 500};
800
801                 SendMessage(Globals.hstatusbar, WM_SIZE, 0, 0);
802                 SendMessage(Globals.hstatusbar, SB_SETPARTS, 2, (LPARAM)&parts);
803                 GetClientRect(Globals.hstatusbar, &rt);
804                 prect->bottom -= rt.bottom;
805         }
806
807         MoveWindow(Globals.hmdiclient, prect->left-1,prect->top-1,prect->right+2,prect->bottom+1, TRUE);
808 }
809
810 static void resize_frame(HWND hwnd, int cx, int cy)
811 {
812         RECT rect;
813
814         rect.left   = 0;
815         rect.top    = 0;
816         rect.right  = cx;
817         rect.bottom = cy;
818
819         resize_frame_rect(hwnd, &rect);
820 }
821
822 static void resize_frame_client(HWND hwnd)
823 {
824         RECT rect;
825
826         GetClientRect(hwnd, &rect);
827
828         resize_frame_rect(hwnd, &rect);
829 }
830
831
832 static HHOOK hcbthook;
833 static ChildWnd* newchild = NULL;
834
835 LRESULT CALLBACK CBTProc(int code, WPARAM wparam, LPARAM lparam)
836 {
837         if (code==HCBT_CREATEWND && newchild) {
838                 ChildWnd* child = newchild;
839                 newchild = NULL;
840
841                 child->hwnd = (HWND) wparam;
842                 SetWindowLong(child->hwnd, GWL_USERDATA, (LPARAM)child);
843         }
844
845         return CallNextHookEx(hcbthook, code, wparam, lparam);
846 }
847
848 static HWND create_child_window(ChildWnd* child)
849 {
850         MDICREATESTRUCT mcs;
851         int idx;
852
853         mcs.szClass = WINEFILETREE;
854         mcs.szTitle = (LPTSTR)child->path;
855         mcs.hOwner  = Globals.hInstance;
856         mcs.x       = child->pos.rcNormalPosition.left;
857         mcs.y       = child->pos.rcNormalPosition.top;
858         mcs.cx      = child->pos.rcNormalPosition.right-child->pos.rcNormalPosition.left;
859         mcs.cy      = child->pos.rcNormalPosition.bottom-child->pos.rcNormalPosition.top;
860         mcs.style   = 0;
861         mcs.lParam  = 0;
862
863         hcbthook = SetWindowsHookEx(WH_CBT, CBTProc, 0, GetCurrentThreadId());
864
865         newchild = child;
866         child->hwnd = (HWND) SendMessage(Globals.hmdiclient, WM_MDICREATE, 0, (LPARAM)&mcs);
867         if (!child->hwnd)
868                 return 0;
869
870         UnhookWindowsHookEx(hcbthook);
871
872         idx = ListBox_FindItemData(child->left.hwnd, ListBox_GetCurSel(child->left.hwnd), child->left.cur);
873         ListBox_SetCurSel(child->left.hwnd, idx);
874
875         return child->hwnd;
876 }
877
878
879 struct ExecuteDialog {
880         TCHAR   cmd[MAX_PATH];
881         int             cmdshow;
882 };
883
884
885 static BOOL CALLBACK ExecuteDialogWndProg(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam)
886 {
887         static struct ExecuteDialog* dlg;
888
889         switch(nmsg) {
890                 case WM_INITDIALOG:
891                         dlg = (struct ExecuteDialog*) lparam;
892                         return 1;
893
894                 case WM_COMMAND: {
895                         int id = (int)wparam;
896
897                         if (id == IDOK) {
898                                 GetWindowText(GetDlgItem(hwnd, 201), dlg->cmd, MAX_PATH);
899                                 dlg->cmdshow = Button_GetState(GetDlgItem(hwnd,214))&BST_CHECKED?
900                                                                                                 SW_SHOWMINIMIZED: SW_SHOWNORMAL;
901                                 EndDialog(hwnd, id);
902                         } else if (id == IDCANCEL)
903                                 EndDialog(hwnd, id);
904
905                         return 1;}
906         }
907
908         return 0;
909 }
910
911
912 #ifndef _NO_EXTENSIONS
913
914 static struct FullScreenParameters {
915         BOOL    mode;
916         RECT    orgPos;
917         BOOL    wasZoomed;
918 } g_fullscreen = {
919         FALSE   /* mode */
920 };
921
922 void frame_get_clientspace(HWND hwnd, PRECT prect)
923 {
924         RECT rt;
925
926         if (!IsIconic(hwnd))
927                 GetClientRect(hwnd, prect);
928         else {
929                 WINDOWPLACEMENT wp;
930
931                 GetWindowPlacement(hwnd, &wp);
932
933                 prect->left = prect->top = 0;
934                 prect->right = wp.rcNormalPosition.right-wp.rcNormalPosition.left-
935                                                 2*(GetSystemMetrics(SM_CXSIZEFRAME)+GetSystemMetrics(SM_CXEDGE));
936                 prect->bottom = wp.rcNormalPosition.bottom-wp.rcNormalPosition.top-
937                                                 2*(GetSystemMetrics(SM_CYSIZEFRAME)+GetSystemMetrics(SM_CYEDGE))-
938                                                 GetSystemMetrics(SM_CYCAPTION)-GetSystemMetrics(SM_CYMENUSIZE);
939         }
940
941         if (IsWindowVisible(Globals.htoolbar)) {
942                 GetClientRect(Globals.htoolbar, &rt);
943                 prect->top += rt.bottom+2;
944         }
945
946         if (IsWindowVisible(Globals.hdrivebar)) {
947                 GetClientRect(Globals.hdrivebar, &rt);
948                 prect->top += rt.bottom+2;
949         }
950
951         if (IsWindowVisible(Globals.hstatusbar)) {
952                 GetClientRect(Globals.hstatusbar, &rt);
953                 prect->bottom -= rt.bottom;
954         }
955 }
956
957 static BOOL toggle_fullscreen(HWND hwnd)
958 {
959         RECT rt;
960
961         if ((g_fullscreen.mode=!g_fullscreen.mode)) {
962                 GetWindowRect(hwnd, &g_fullscreen.orgPos);
963                 g_fullscreen.wasZoomed = IsZoomed(hwnd);
964
965                 Frame_CalcFrameClient(hwnd, &rt);
966                 ClientToScreen(hwnd, (LPPOINT)&rt.left);
967                 ClientToScreen(hwnd, (LPPOINT)&rt.right);
968
969                 rt.left = g_fullscreen.orgPos.left-rt.left;
970                 rt.top = g_fullscreen.orgPos.top-rt.top;
971                 rt.right = GetSystemMetrics(SM_CXSCREEN)+g_fullscreen.orgPos.right-rt.right;
972                 rt.bottom = GetSystemMetrics(SM_CYSCREEN)+g_fullscreen.orgPos.bottom-rt.bottom;
973
974                 MoveWindow(hwnd, rt.left, rt.top, rt.right-rt.left, rt.bottom-rt.top, TRUE);
975         } else {
976                 MoveWindow(hwnd, g_fullscreen.orgPos.left, g_fullscreen.orgPos.top,
977                                                         g_fullscreen.orgPos.right-g_fullscreen.orgPos.left,
978                                                         g_fullscreen.orgPos.bottom-g_fullscreen.orgPos.top, TRUE);
979
980                 if (g_fullscreen.wasZoomed)
981                         ShowWindow(hwnd, WS_MAXIMIZE);
982         }
983
984         return g_fullscreen.mode;
985 }
986
987 static void fullscreen_move(HWND hwnd)
988 {
989         RECT rt, pos;
990         GetWindowRect(hwnd, &pos);
991
992         Frame_CalcFrameClient(hwnd, &rt);
993         ClientToScreen(hwnd, (LPPOINT)&rt.left);
994         ClientToScreen(hwnd, (LPPOINT)&rt.right);
995
996         rt.left = pos.left-rt.left;
997         rt.top = pos.top-rt.top;
998         rt.right = GetSystemMetrics(SM_CXSCREEN)+pos.right-rt.right;
999         rt.bottom = GetSystemMetrics(SM_CYSCREEN)+pos.bottom-rt.bottom;
1000
1001         MoveWindow(hwnd, rt.left, rt.top, rt.right-rt.left, rt.bottom-rt.top, TRUE);
1002 }
1003
1004 #endif
1005
1006
1007 static void toggle_child(HWND hwnd, UINT cmd, HWND hchild)
1008 {
1009         BOOL vis = IsWindowVisible(hchild);
1010
1011         CheckMenuItem(Globals.hMenuOptions, cmd, vis?MF_BYCOMMAND:MF_BYCOMMAND|MF_CHECKED);
1012
1013         ShowWindow(hchild, vis?SW_HIDE:SW_SHOW);
1014
1015 #ifndef _NO_EXTENSIONS
1016         if (g_fullscreen.mode)
1017                 fullscreen_move(hwnd);
1018 #endif
1019
1020         resize_frame_client(hwnd);
1021 }
1022
1023 BOOL activate_drive_window(LPCTSTR path)
1024 {
1025         TCHAR drv1[_MAX_DRIVE], drv2[_MAX_DRIVE];
1026         HWND child_wnd;
1027
1028         _tsplitpath(path, drv1, 0, 0, 0);
1029
1030         /* search for a already open window for the same drive */
1031         for(child_wnd=GetNextWindow(Globals.hmdiclient,GW_CHILD); child_wnd; child_wnd=GetNextWindow(child_wnd, GW_HWNDNEXT)) {
1032                 ChildWnd* child = (ChildWnd*) GetWindowLong(child_wnd, GWL_USERDATA);
1033
1034                 if (child) {
1035                         _tsplitpath(child->root.path, drv2, 0, 0, 0);
1036
1037                         if (!lstrcmpi(drv2, drv1)) {
1038                                 SendMessage(Globals.hmdiclient, WM_MDIACTIVATE, (WPARAM)child_wnd, 0);
1039
1040                                 if (IsMinimized(child_wnd))
1041                                         ShowWindow(child_wnd, SW_SHOWNORMAL);
1042
1043                                 return TRUE;
1044                         }
1045                 }
1046         }
1047
1048         return FALSE;
1049 }
1050
1051 LRESULT CALLBACK FrameWndProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam)
1052 {
1053         switch(nmsg) {
1054                 case WM_CLOSE:
1055                         DestroyWindow(hwnd);
1056                         break;
1057
1058                 case WM_DESTROY:
1059                         PostQuitMessage(0);
1060                         break;
1061
1062                 case WM_COMMAND: {
1063                         UINT cmd = LOWORD(wparam);
1064                         HWND hwndClient = (HWND) SendMessage(Globals.hmdiclient, WM_MDIGETACTIVE, 0, 0);
1065
1066                         if (SendMessage(hwndClient, WM_DISPATCH_COMMAND, wparam, lparam))
1067                                 break;
1068
1069                         if (cmd>=ID_DRIVE_FIRST && cmd<=ID_DRIVE_FIRST+0xFF) {
1070                                 TCHAR drv[_MAX_DRIVE], path[MAX_PATH];
1071                                 ChildWnd* child;
1072                                 LPCTSTR root = Globals.drives;
1073                                 int i;
1074
1075                                 for(i=cmd-ID_DRIVE_FIRST; i--; root++)
1076                                         while(*root)
1077                                                 root++;
1078
1079                                 if (activate_drive_window(root))
1080                                         return 0;
1081
1082                                 _tsplitpath(root, drv, 0, 0, 0);
1083
1084                                 if (!SetCurrentDirectory(drv)) {
1085                                         display_error(hwnd, GetLastError());
1086                                         return 0;
1087                                 }
1088
1089                                 GetCurrentDirectory(MAX_PATH, path); /*@@ letztes Verzeichnis pro Laufwerk speichern */
1090                                 child = alloc_child_window(path);
1091
1092                                 if (!create_child_window(child))
1093                                         free(child);
1094                         } else switch(cmd) {
1095                                 case ID_FILE_EXIT:
1096                                         PostQuitMessage(0);
1097                                         break;
1098
1099                                 case ID_WINDOW_NEW: {
1100                                         TCHAR path[MAX_PATH];
1101                                         ChildWnd* child;
1102
1103                                         GetCurrentDirectory(MAX_PATH, path);
1104                                         child = alloc_child_window(path);
1105
1106                                         if (!create_child_window(child))
1107                                                 free(child);
1108                                         break;}
1109
1110                                 case ID_WINDOW_CASCADE:
1111                                         SendMessage(Globals.hmdiclient, WM_MDICASCADE, 0, 0);
1112                                         break;
1113
1114                                 case ID_WINDOW_TILE_HORZ:
1115                                         SendMessage(Globals.hmdiclient, WM_MDITILE, MDITILE_HORIZONTAL, 0);
1116                                         break;
1117
1118                                 case ID_WINDOW_TILE_VERT:
1119                                         SendMessage(Globals.hmdiclient, WM_MDITILE, MDITILE_VERTICAL, 0);
1120                                         break;
1121
1122                                 case ID_WINDOW_ARRANGE:
1123                                         SendMessage(Globals.hmdiclient, WM_MDIICONARRANGE, 0, 0);
1124                                         break;
1125
1126                                 case ID_VIEW_TOOL_BAR:
1127                                         toggle_child(hwnd, cmd, Globals.htoolbar);
1128                                         break;
1129
1130                                 case ID_VIEW_DRIVE_BAR:
1131                                         toggle_child(hwnd, cmd, Globals.hdrivebar);
1132                                         break;
1133
1134                                 case ID_VIEW_STATUSBAR:
1135                                         toggle_child(hwnd, cmd, Globals.hstatusbar);
1136                                         break;
1137
1138                                 case ID_EXECUTE: {
1139                                         struct ExecuteDialog dlg = {{0}};
1140                                         if (DialogBoxParam(Globals.hInstance, MAKEINTRESOURCE(IDD_EXECUTE), hwnd, ExecuteDialogWndProg, (LPARAM)&dlg) == IDOK)
1141                                                 ShellExecute(hwnd, _T("open")/*operation*/, dlg.cmd/*file*/, NULL/*parameters*/, NULL/*dir*/, dlg.cmdshow);
1142                                         break;}
1143
1144                                 case ID_HELP:
1145                                         WinHelp(hwnd, _T("winfile"), HELP_INDEX, 0);
1146                                         break;
1147
1148 #ifndef _NO_EXTENSIONS
1149                                 case ID_VIEW_FULLSCREEN:
1150                                         CheckMenuItem(Globals.hMenuOptions, cmd, toggle_fullscreen(hwnd)?MF_CHECKED:0);
1151                                         break;
1152
1153 #ifdef __linux__
1154                                 case ID_DRIVE_UNIX_FS: {
1155                                         TCHAR path[MAX_PATH];
1156                                         ChildWnd* child;
1157
1158                                         if (activate_drive_window(_T("/")))
1159                                                 break;
1160
1161                                         getcwd(path, MAX_PATH);
1162                                         child = alloc_child_window(path);
1163
1164                                         if (!create_child_window(child))
1165                                                 free(child);
1166                                         break;}
1167 #endif
1168 #endif
1169
1170                                 /*TODO: There are even more menu items! */
1171
1172 #ifndef _NO_EXTENSIONS
1173                                 case ID_LICENSE:
1174                                         WineLicense(Globals.hMainWnd);
1175                                         break;
1176
1177                                 case ID_NO_WARRANTY:
1178                                         WineWarranty(Globals.hMainWnd);
1179                                         break;
1180
1181                                 case ID_ABOUT_WINE:
1182                                         ShellAbout(hwnd, _T("WINE"), _T("Winefile"), 0);
1183                                         break;
1184 #endif
1185
1186                                 default:
1187                                         /*@@if (wParam >= PM_FIRST_LANGUAGE && wParam <= PM_LAST_LANGUAGE)
1188                                                 STRING_SelectLanguageByNumber(wParam - PM_FIRST_LANGUAGE);
1189                                         else */if ((cmd<IDW_FIRST_CHILD || cmd>=IDW_FIRST_CHILD+0x100) &&
1190                                                 (cmd<SC_SIZE || cmd>SC_RESTORE))
1191                                                 MessageBox(hwnd, _T("Not yet implemented"), _T("Winefile"), MB_OK);
1192
1193                                         return DefFrameProc(hwnd, Globals.hmdiclient, nmsg, wparam, lparam);
1194                         }
1195                         break;}
1196
1197                 case WM_SIZE:
1198                         resize_frame(hwnd, LOWORD(lparam), HIWORD(lparam));
1199                         break;  /* do not pass message to DefFrameProc */
1200
1201 #ifndef _NO_EXTENSIONS
1202                 case WM_GETMINMAXINFO: {
1203                         LPMINMAXINFO lpmmi = (LPMINMAXINFO)lparam;
1204
1205                         lpmmi->ptMaxTrackSize.x <<= 1;/*2*GetSystemMetrics(SM_CXSCREEN) / SM_CXVIRTUALSCREEN */
1206                         lpmmi->ptMaxTrackSize.y <<= 1;/*2*GetSystemMetrics(SM_CYSCREEN) / SM_CYVIRTUALSCREEN */
1207                         break;}
1208
1209                 case FRM_CALC_CLIENT:
1210                         frame_get_clientspace(hwnd, (PRECT)lparam);
1211                         return TRUE;
1212 #endif
1213
1214                 default:
1215                         return DefFrameProc(hwnd, Globals.hmdiclient, nmsg, wparam, lparam);
1216         }
1217
1218         return 0;
1219 }
1220
1221
1222 const static LPTSTR g_pos_names[COLUMNS] = {
1223         _T(""),                 /* symbol */
1224         _T("Name"),
1225         _T("Size"),
1226         _T("CDate"),
1227 #ifndef _NO_EXTENSIONS
1228         _T("ADate"),
1229         _T("MDate"),
1230         _T("Index/Inode"),
1231         _T("Links"),
1232 #endif
1233         _T("Attributes"),
1234 #ifndef _NO_EXTENSIONS
1235         _T("Security")
1236 #endif
1237 };
1238
1239 const static int g_pos_align[] = {
1240         0,
1241         HDF_LEFT,       /* Name */
1242         HDF_RIGHT,      /* Size */
1243         HDF_LEFT,       /* CDate */
1244 #ifndef _NO_EXTENSIONS
1245         HDF_LEFT,       /* ADate */
1246         HDF_LEFT,       /* MDate */
1247         HDF_LEFT,       /* Index */
1248         HDF_CENTER,     /* Links */
1249 #endif
1250         HDF_CENTER,     /* Attributes */
1251 #ifndef _NO_EXTENSIONS
1252         HDF_LEFT        /* Security */
1253 #endif
1254 };
1255
1256 static void resize_tree(ChildWnd* child, int cx, int cy)
1257 {
1258         HDWP hdwp = BeginDeferWindowPos(4);
1259         RECT rt;
1260
1261         rt.left   = 0;
1262         rt.top    = 0;
1263         rt.right  = cx;
1264         rt.bottom = cy;
1265
1266         cx = child->split_pos + SPLIT_WIDTH/2;
1267
1268 #ifndef _NO_EXTENSIONS
1269         {
1270                 WINDOWPOS wp;
1271                 HD_LAYOUT hdl;
1272
1273                 hdl.prc   = &rt;
1274                 hdl.pwpos = &wp;
1275
1276                 Header_Layout(child->left.hwndHeader, &hdl);
1277
1278                 DeferWindowPos(hdwp, child->left.hwndHeader, wp.hwndInsertAfter,
1279                                                 wp.x-1, wp.y, child->split_pos-SPLIT_WIDTH/2+1, wp.cy, wp.flags);
1280                 DeferWindowPos(hdwp, child->right.hwndHeader, wp.hwndInsertAfter,
1281                                                 rt.left+cx+1, wp.y, wp.cx-cx+2, wp.cy, wp.flags);
1282         }
1283 #endif
1284
1285         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);
1286         DeferWindowPos(hdwp, child->right.hwnd, 0, rt.left+cx+1, rt.top, rt.right-cx, rt.bottom-rt.top, SWP_NOZORDER|SWP_NOACTIVATE);
1287
1288         EndDeferWindowPos(hdwp);
1289 }
1290
1291
1292 #ifndef _NO_EXTENSIONS
1293
1294 static HWND create_header(HWND parent, Pane* pane, int id)
1295 {
1296         HD_ITEM hdi = {HDI_TEXT|HDI_WIDTH|HDI_FORMAT};
1297         int idx;
1298
1299         HWND hwnd = CreateWindow(WC_HEADER, 0, WS_CHILD|WS_VISIBLE|HDS_HORZ/*TODO: |HDS_BUTTONS + sort orders*/,
1300                                                                 0, 0, 0, 0, parent, (HMENU)id, Globals.hInstance, 0);
1301         if (!hwnd)
1302                 return 0;
1303
1304         SendMessage(hwnd, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), FALSE);
1305
1306         for(idx=0; idx<COLUMNS; idx++) {
1307                 hdi.pszText = g_pos_names[idx];
1308                 hdi.fmt = HDF_STRING | g_pos_align[idx];
1309                 hdi.cxy = pane->widths[idx];
1310                 Header_InsertItem(hwnd, idx, &hdi);
1311         }
1312
1313         return hwnd;
1314 }
1315
1316 #endif
1317
1318
1319 static void init_output(HWND hwnd)
1320 {
1321         TCHAR b[16];
1322         HFONT old_font;
1323         HDC hdc = GetDC(hwnd);
1324
1325         if (GetNumberFormat(LOCALE_USER_DEFAULT, 0, _T("1000"), 0, b, 16) > 4)
1326                 Globals.num_sep = b[1];
1327         else
1328                 Globals.num_sep = _T('.');
1329
1330         old_font = SelectFont(hdc, Globals.hfont);
1331         GetTextExtentPoint32(hdc, _T(" "), 1, &Globals.spaceSize);
1332         SelectFont(hdc, old_font);
1333         ReleaseDC(hwnd, hdc);
1334 }
1335
1336 static void draw_item(Pane* pane, LPDRAWITEMSTRUCT dis, Entry* entry, int calcWidthCol);
1337
1338
1339 /* calculate prefered width for all visible columns */
1340
1341 static BOOL calc_widths(Pane* pane, BOOL anyway)
1342 {
1343         int col, x, cx, spc=3*Globals.spaceSize.cx;
1344         int entries = ListBox_GetCount(pane->hwnd);
1345         int orgWidths[COLUMNS];
1346         int orgPositions[COLUMNS+1];
1347         HFONT hfontOld;
1348         HDC hdc;
1349         int cnt;
1350
1351         if (!anyway) {
1352                 memcpy(orgWidths, pane->widths, sizeof(orgWidths));
1353                 memcpy(orgPositions, pane->positions, sizeof(orgPositions));
1354         }
1355
1356         for(col=0; col<COLUMNS; col++)
1357                 pane->widths[col] = 0;
1358
1359         hdc = GetDC(pane->hwnd);
1360         hfontOld = SelectFont(hdc, Globals.hfont);
1361
1362         for(cnt=0; cnt<entries; cnt++) {
1363                 Entry* entry = (Entry*) ListBox_GetItemData(pane->hwnd, cnt);
1364
1365                 DRAWITEMSTRUCT dis;
1366
1367                 dis.CtlType    = 0;
1368                 dis.CtlID      = 0;
1369                 dis.itemID     = 0;
1370                 dis.itemAction = 0;
1371                 dis.itemState  = 0;
1372                 dis.hwndItem   = pane->hwnd;
1373                 dis.hDC        = hdc;
1374
1375                 draw_item(pane, &dis, entry, COLUMNS);
1376         }
1377
1378         SelectObject(hdc, hfontOld);
1379         ReleaseDC(pane->hwnd, hdc);
1380
1381         x = 0;
1382         for(col=0; col<COLUMNS; col++) {
1383                 pane->positions[col] = x;
1384                 cx = pane->widths[col];
1385
1386                 if (cx) {
1387                         cx += spc;
1388
1389                         if (cx < IMAGE_WIDTH)
1390                                 cx = IMAGE_WIDTH;
1391
1392                         pane->widths[col] = cx;
1393                 }
1394
1395                 x += cx;
1396         }
1397
1398         pane->positions[COLUMNS] = x;
1399
1400         ListBox_SetHorizontalExtent(pane->hwnd, x);
1401
1402         /* no change? */
1403         if (!memcmp(orgWidths, pane->widths, sizeof(orgWidths)))
1404                 return FALSE;
1405
1406         /* don't move, if only collapsing an entry */
1407         if (!anyway && pane->widths[0]<orgWidths[0] &&
1408                 !memcmp(orgWidths+1, pane->widths+1, sizeof(orgWidths)-sizeof(int))) {
1409                 pane->widths[0] = orgWidths[0];
1410                 memcpy(pane->positions, orgPositions, sizeof(orgPositions));
1411
1412                 return FALSE;
1413         }
1414
1415         InvalidateRect(pane->hwnd, 0, TRUE);
1416
1417         return TRUE;
1418 }
1419
1420
1421 /* calculate one prefered column width */
1422
1423 static void calc_single_width(Pane* pane, int col)
1424 {
1425         HFONT hfontOld;
1426         int x, cx;
1427         int entries = ListBox_GetCount(pane->hwnd);
1428         int cnt;
1429         HDC hdc;
1430
1431         pane->widths[col] = 0;
1432
1433         hdc = GetDC(pane->hwnd);
1434         hfontOld = SelectFont(hdc, Globals.hfont);
1435
1436         for(cnt=0; cnt<entries; cnt++) {
1437                 Entry* entry = (Entry*) ListBox_GetItemData(pane->hwnd, cnt);
1438                 DRAWITEMSTRUCT dis;
1439
1440                 dis.CtlType    = 0;
1441                 dis.CtlID      = 0;
1442                 dis.itemID     = 0;
1443                 dis.itemAction = 0;
1444                 dis.itemState  = 0;
1445                 dis.hwndItem   = pane->hwnd;
1446                 dis.hDC        = hdc;
1447
1448                 draw_item(pane, &dis, entry, col);
1449         }
1450
1451         SelectObject(hdc, hfontOld);
1452         ReleaseDC(pane->hwnd, hdc);
1453
1454         cx = pane->widths[col];
1455
1456         if (cx) {
1457                 cx += 3*Globals.spaceSize.cx;
1458
1459                 if (cx < IMAGE_WIDTH)
1460                         cx = IMAGE_WIDTH;
1461         }
1462
1463         pane->widths[col] = cx;
1464
1465         x = pane->positions[col] + cx;
1466
1467         for(; col<COLUMNS; ) {
1468                 pane->positions[++col] = x;
1469                 x += pane->widths[col];
1470         }
1471
1472         ListBox_SetHorizontalExtent(pane->hwnd, x);
1473 }
1474
1475
1476 /* insert listbox entries after index idx */
1477
1478 static void insert_entries(Pane* pane, Entry* parent, int idx)
1479 {
1480         Entry* entry = parent;
1481
1482         if (!entry)
1483                 return;
1484
1485         ShowWindow(pane->hwnd, SW_HIDE);
1486
1487         for(; entry; entry=entry->next) {
1488 #ifndef _LEFT_FILES
1489                 if (pane->treePane && !(entry->data.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY))
1490                         continue;
1491 #endif
1492
1493                 /* don't display entries "." and ".." in the left pane */
1494                 if (pane->treePane && (entry->data.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
1495                                 && entry->data.cFileName[0]==_T('.'))
1496                         if (
1497 #ifndef _NO_EXTENSIONS
1498                                 entry->data.cFileName[1]==_T('\0') ||
1499 #endif
1500                                 (entry->data.cFileName[1]==_T('.') && entry->data.cFileName[2]==_T('\0')))
1501                                 continue;
1502
1503                 if (idx != -1)
1504                         idx++;
1505
1506                 ListBox_InsertItemData(pane->hwnd, idx, entry);
1507
1508                 if (pane->treePane && entry->expanded)
1509                         insert_entries(pane, entry->down, idx);
1510         }
1511
1512         ShowWindow(pane->hwnd, SW_SHOW);
1513 }
1514
1515
1516 static WNDPROC g_orgTreeWndProc;
1517
1518 static void create_tree_window(HWND parent, Pane* pane, int id, int id_header)
1519 {
1520         static int s_init = 0;
1521         Entry* entry = pane->root;
1522
1523         pane->hwnd = CreateWindow(_T("ListBox"), _T(""), WS_CHILD|WS_VISIBLE|WS_HSCROLL|WS_VSCROLL|
1524                                                                 LBS_DISABLENOSCROLL|LBS_NOINTEGRALHEIGHT|LBS_OWNERDRAWFIXED|LBS_NOTIFY,
1525                                                                 0, 0, 0, 0, parent, (HMENU)id, Globals.hInstance, 0);
1526
1527         SetWindowLong(pane->hwnd, GWL_USERDATA, (LPARAM)pane);
1528         g_orgTreeWndProc = SubclassWindow(pane->hwnd, TreeWndProc);
1529
1530         SendMessage(pane->hwnd, WM_SETFONT, (WPARAM)Globals.hfont, FALSE);
1531
1532         /* insert entries into listbox */
1533         if (entry)
1534                 insert_entries(pane, entry, -1);
1535
1536         /* calculate column widths */
1537         if (!s_init) {
1538                 s_init = 1;
1539                 init_output(pane->hwnd);
1540         }
1541
1542         calc_widths(pane, TRUE);
1543
1544 #ifndef _NO_EXTENSIONS
1545         pane->hwndHeader = create_header(parent, pane, id_header);
1546 #endif
1547 }
1548
1549
1550 static void InitChildWindow(ChildWnd* child)
1551 {
1552         create_tree_window(child->hwnd, &child->left, IDW_TREE_LEFT, IDW_HEADER_LEFT);
1553         create_tree_window(child->hwnd, &child->right, IDW_TREE_RIGHT, IDW_HEADER_RIGHT);
1554 }
1555
1556
1557 static void format_date(const FILETIME* ft, TCHAR* buffer, int visible_cols)
1558 {
1559         SYSTEMTIME systime;
1560         FILETIME lft;
1561         int len = 0;
1562
1563         *buffer = _T('\0');
1564
1565         if (!ft->dwLowDateTime && !ft->dwHighDateTime)
1566                 return;
1567
1568         if (!FileTimeToLocalFileTime(ft, &lft))
1569                 {err: _tcscpy(buffer,_T("???")); return;}
1570
1571         if (!FileTimeToSystemTime(&lft, &systime))
1572                 goto err;
1573
1574         if (visible_cols & COL_DATE) {
1575                 len = GetDateFormat(LOCALE_USER_DEFAULT, 0, &systime, 0, buffer, BUFFER_LEN);
1576                 if (!len)
1577                         goto err;
1578         }
1579
1580         if (visible_cols & COL_TIME) {
1581                 if (len)
1582                         buffer[len-1] = ' ';
1583
1584                 buffer[len++] = ' ';
1585
1586                 if (!GetTimeFormat(LOCALE_USER_DEFAULT, 0, &systime, 0, buffer+len, BUFFER_LEN-len))
1587                         buffer[len] = _T('\0');
1588         }
1589 }
1590
1591
1592 static void calc_width(Pane* pane, LPDRAWITEMSTRUCT dis, int col, LPCTSTR str)
1593 {
1594         RECT rt = {0};
1595
1596         DrawText(dis->hDC, (LPTSTR)str, -1, &rt, DT_CALCRECT|DT_SINGLELINE|DT_NOPREFIX);
1597
1598         if (rt.right > pane->widths[col])
1599                 pane->widths[col] = rt.right;
1600 }
1601
1602 static void calc_tabbed_width(Pane* pane, LPDRAWITEMSTRUCT dis, int col, LPCTSTR str)
1603 {
1604         RECT rt = {0};
1605
1606 /*      DRAWTEXTPARAMS dtp = {sizeof(DRAWTEXTPARAMS), 2};
1607         DrawTextEx(dis->hDC, (LPTSTR)str, -1, &rt, DT_CALCRECT|DT_SINGLELINE|DT_NOPREFIX|DT_EXPANDTABS|DT_TABSTOP, &dtp);*/
1608
1609         DrawText(dis->hDC, (LPTSTR)str, -1, &rt, DT_CALCRECT|DT_SINGLELINE|DT_EXPANDTABS|DT_TABSTOP|(2<<8));
1610         /*@@ rt (0,0) ??? */
1611
1612         if (rt.right > pane->widths[col])
1613                 pane->widths[col] = rt.right;
1614 }
1615
1616
1617 static void output_text(Pane* pane, LPDRAWITEMSTRUCT dis, int col, LPCTSTR str, DWORD flags)
1618 {
1619         int x = dis->rcItem.left;
1620         RECT rt;
1621
1622         rt.left   = x+pane->positions[col]+Globals.spaceSize.cx;
1623         rt.top    = dis->rcItem.top;
1624         rt.right  = x+pane->positions[col+1]-Globals.spaceSize.cx;
1625         rt.bottom = dis->rcItem.bottom;
1626
1627         DrawText(dis->hDC, (LPTSTR)str, -1, &rt, DT_SINGLELINE|DT_NOPREFIX|flags);
1628 }
1629
1630 static void output_tabbed_text(Pane* pane, LPDRAWITEMSTRUCT dis, int col, LPCTSTR str)
1631 {
1632         int x = dis->rcItem.left;
1633         RECT rt;
1634
1635         rt.left   = x+pane->positions[col]+Globals.spaceSize.cx;
1636         rt.top    = dis->rcItem.top;
1637         rt.right  = x+pane->positions[col+1]-Globals.spaceSize.cx;
1638         rt.bottom = dis->rcItem.bottom;
1639
1640 /*      DRAWTEXTPARAMS dtp = {sizeof(DRAWTEXTPARAMS), 2};
1641         DrawTextEx(dis->hDC, (LPTSTR)str, -1, &rt, DT_SINGLELINE|DT_NOPREFIX|DT_EXPANDTABS|DT_TABSTOP, &dtp);*/
1642
1643         DrawText(dis->hDC, (LPTSTR)str, -1, &rt, DT_SINGLELINE|DT_EXPANDTABS|DT_TABSTOP|(2<<8));
1644 }
1645
1646 static void output_number(Pane* pane, LPDRAWITEMSTRUCT dis, int col, LPCTSTR str)
1647 {
1648         int x = dis->rcItem.left;
1649         RECT rt;
1650         LPCTSTR s = str;
1651         TCHAR b[128];
1652         LPTSTR d = b;
1653         int pos;
1654
1655         rt.left   = x+pane->positions[col]+Globals.spaceSize.cx;
1656         rt.top    = dis->rcItem.top;
1657         rt.right  = x+pane->positions[col+1]-Globals.spaceSize.cx;
1658         rt.bottom = dis->rcItem.bottom;
1659
1660         if (*s)
1661                 *d++ = *s++;
1662
1663         /* insert number separator characters */
1664         pos = lstrlen(s) % 3;
1665
1666         while(*s)
1667                 if (pos--)
1668                         *d++ = *s++;
1669                 else {
1670                         *d++ = Globals.num_sep;
1671                         pos = 3;
1672                 }
1673
1674         DrawText(dis->hDC, b, d-b, &rt, DT_RIGHT|DT_SINGLELINE|DT_NOPREFIX|DT_END_ELLIPSIS);
1675 }
1676
1677
1678 static int is_exe_file(LPCTSTR ext)
1679 {
1680         const static LPCTSTR executable_extensions[] = {
1681                 _T("COM"),
1682                 _T("EXE"),
1683                 _T("BAT"),
1684                 _T("CMD"),
1685 #ifndef _NO_EXTENSIONS
1686                 _T("CMM"),
1687                 _T("BTM"),
1688                 _T("AWK"),
1689 #endif
1690                 0
1691         };
1692
1693         TCHAR ext_buffer[_MAX_EXT];
1694         const LPCTSTR* p;
1695         LPCTSTR s;
1696         LPTSTR d;
1697
1698         for(s=ext+1,d=ext_buffer; (*d=tolower(*s)); s++)
1699                 d++;
1700
1701         for(p=executable_extensions; *p; p++)
1702                 if (!_tcscmp(ext_buffer, *p))
1703                         return 1;
1704
1705         return 0;
1706 }
1707
1708 static int is_registered_type(LPCTSTR ext)
1709 {
1710         /* TODO */
1711
1712         return 1;
1713 }
1714
1715
1716 static void draw_item(Pane* pane, LPDRAWITEMSTRUCT dis, Entry* entry, int calcWidthCol)
1717 {
1718         TCHAR buffer[BUFFER_LEN];
1719         DWORD attrs;
1720         int visible_cols = pane->visible_cols;
1721         COLORREF bkcolor, textcolor;
1722         RECT focusRect = dis->rcItem;
1723         HBRUSH hbrush;
1724         enum IMAGE img;
1725         int img_pos, cx;
1726         int col = 0;
1727
1728         if (entry) {
1729                 attrs = entry->data.dwFileAttributes;
1730
1731                 if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
1732                         if (entry->data.cFileName[0]==_T('.') && entry->data.cFileName[1]==_T('.')
1733                                         && entry->data.cFileName[2]==_T('\0'))
1734                                 img = IMG_FOLDER_UP;
1735 #ifndef _NO_EXTENSIONS
1736                         else if (entry->data.cFileName[0]==_T('.') && entry->data.cFileName[1]==_T('\0'))
1737                                 img = IMG_FOLDER_CUR;
1738 #endif
1739                         else if (
1740 #ifdef _NO_EXTENSIONS
1741                                          entry->expanded ||
1742 #endif
1743                                          (pane->treePane && (dis->itemState&ODS_FOCUS)))
1744                                 img = IMG_OPEN_FOLDER;
1745                         else
1746                                 img = IMG_FOLDER;
1747                 } else {
1748                         LPCTSTR ext = _tcsrchr(entry->data.cFileName, '.');
1749                         if (!ext)
1750                                 ext = _T("");
1751
1752                         if (is_exe_file(ext))
1753                                 img = IMG_EXECUTABLE;
1754                         else if (is_registered_type(ext))
1755                                 img = IMG_DOCUMENT;
1756                         else
1757                                 img = IMG_FILE;
1758                 }
1759         } else {
1760                 attrs = 0;
1761                 img = IMG_NONE;
1762         }
1763
1764         if (pane->treePane) {
1765                 if (entry) {
1766                         img_pos = dis->rcItem.left + entry->level*(IMAGE_WIDTH+Globals.spaceSize.cx);
1767
1768                         if (calcWidthCol == -1) {
1769                                 int x;
1770                                 int y = dis->rcItem.top + IMAGE_HEIGHT/2;
1771                                 Entry* up;
1772                                 RECT rt_clip;
1773                                 HRGN hrgn_org = CreateRectRgn(0, 0, 0, 0);
1774                                 HRGN hrgn;
1775
1776                                 rt_clip.left   = dis->rcItem.left;
1777                                 rt_clip.top    = dis->rcItem.top;
1778                                 rt_clip.right  = dis->rcItem.left+pane->widths[col];
1779                                 rt_clip.bottom = dis->rcItem.bottom;
1780
1781                                 hrgn = CreateRectRgnIndirect(&rt_clip);
1782
1783                                 if (!GetClipRgn(dis->hDC, hrgn_org)) {
1784                                         DeleteObject(hrgn_org);
1785                                         hrgn_org = 0;
1786                                 }
1787
1788                                 /*                              HGDIOBJ holdPen = SelectObject(dis->hDC, GetStockObject(BLACK_PEN)); */
1789                                 ExtSelectClipRgn(dis->hDC, hrgn, RGN_AND);
1790                                 DeleteObject(hrgn);
1791
1792                                 if ((up=entry->up) != NULL) {
1793                                         MoveToEx(dis->hDC, img_pos-IMAGE_WIDTH/2, y, 0);
1794                                         LineTo(dis->hDC, img_pos-2, y);
1795
1796                                         x = img_pos - IMAGE_WIDTH/2;
1797
1798                                         do {
1799                                                 x -= IMAGE_WIDTH+Globals.spaceSize.cx;
1800
1801                                                 if (up->next
1802 #ifndef _LEFT_FILES
1803                                                         && (up->next->data.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
1804 #endif
1805                                                         ) {
1806                                                         MoveToEx(dis->hDC, x, dis->rcItem.top, 0);
1807                                                         LineTo(dis->hDC, x, dis->rcItem.bottom);
1808                                                 }
1809                                         } while((up=up->up) != NULL);
1810                                 }
1811
1812                                 x = img_pos - IMAGE_WIDTH/2;
1813
1814                                 MoveToEx(dis->hDC, x, dis->rcItem.top, 0);
1815                                 LineTo(dis->hDC, x, y);
1816
1817                                 if (entry->next
1818 #ifndef _LEFT_FILES
1819                                         && (entry->next->data.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
1820 #endif
1821                                         )
1822                                         LineTo(dis->hDC, x, dis->rcItem.bottom);
1823
1824                                 if (entry->down && entry->expanded) {
1825                                         x += IMAGE_WIDTH+Globals.spaceSize.cx;
1826                                         MoveToEx(dis->hDC, x, dis->rcItem.top+IMAGE_HEIGHT, 0);
1827                                         LineTo(dis->hDC, x, dis->rcItem.bottom);
1828                                 }
1829
1830                                 SelectClipRgn(dis->hDC, hrgn_org);
1831                                 if (hrgn_org) DeleteObject(hrgn_org);
1832                                 /*                              SelectObject(dis->hDC, holdPen); */
1833                         } else if (calcWidthCol==col || calcWidthCol==COLUMNS) {
1834                                 int right = img_pos + IMAGE_WIDTH - Globals.spaceSize.cx;
1835
1836                                 if (right > pane->widths[col])
1837                                         pane->widths[col] = right;
1838                         }
1839                 } else  {
1840                         img_pos = dis->rcItem.left;
1841                 }
1842         } else {
1843                 img_pos = dis->rcItem.left;
1844
1845                 if (calcWidthCol==col || calcWidthCol==COLUMNS)
1846                         pane->widths[col] = IMAGE_WIDTH;
1847         }
1848
1849         if (calcWidthCol == -1) {
1850                 focusRect.left = img_pos -2;
1851
1852 #ifdef _NO_EXTENSIONS
1853                 if (pane->treePane && entry) {
1854                         RECT rt = {0};
1855
1856                         DrawText(dis->hDC, entry->data.cFileName, -1, &rt, DT_CALCRECT|DT_SINGLELINE|DT_NOPREFIX);
1857
1858                         focusRect.right = dis->rcItem.left+pane->positions[col+1]+Globals.spaceSize.cx + rt.right +2;
1859                 }
1860 #else
1861
1862                 if (attrs & FILE_ATTRIBUTE_COMPRESSED)
1863                         textcolor = COLOR_COMPRESSED;
1864                 else
1865 #endif
1866                         textcolor = RGB(0,0,0);
1867
1868                 if (dis->itemState & ODS_FOCUS) {
1869                         textcolor = RGB(255,255,255);
1870                         bkcolor = COLOR_SELECTION;
1871                 } else {
1872                         bkcolor = RGB(255,255,255);
1873                 }
1874
1875                 hbrush = CreateSolidBrush(bkcolor);
1876                 FillRect(dis->hDC, &focusRect, hbrush);
1877                 DeleteObject(hbrush);
1878
1879                 SetBkMode(dis->hDC, TRANSPARENT);
1880                 SetTextColor(dis->hDC, textcolor);
1881
1882                 cx = pane->widths[col];
1883
1884                 if (cx && img!=IMG_NONE) {
1885                         if (cx > IMAGE_WIDTH)
1886                                 cx = IMAGE_WIDTH;
1887
1888                         ImageList_DrawEx(Globals.himl, img, dis->hDC,
1889                                                                 img_pos, dis->rcItem.top, cx,
1890                                                                 IMAGE_HEIGHT, bkcolor, CLR_DEFAULT, ILD_NORMAL);
1891                 }
1892         }
1893
1894         if (!entry)
1895                 return;
1896
1897 #ifdef _NO_EXTENSIONS
1898         if (img >= IMG_FOLDER_UP)
1899                 return;
1900 #endif
1901
1902         col++;
1903
1904         /* ouput file name */
1905         if (calcWidthCol == -1)
1906                 output_text(pane, dis, col, entry->data.cFileName, 0);
1907         else if (calcWidthCol==col || calcWidthCol==COLUMNS)
1908                 calc_width(pane, dis, col, entry->data.cFileName);
1909
1910         col++;
1911
1912 #ifdef _NO_EXTENSIONS
1913   if (!pane->treePane) {
1914 #endif
1915
1916         /* display file size */
1917         if (visible_cols & COL_SIZE) {
1918 #ifdef _NO_EXTENSIONS
1919                 if (!(attrs&FILE_ATTRIBUTE_DIRECTORY))
1920 #endif
1921                 {
1922                         ULONGLONG size;
1923
1924                         size = ((ULONGLONG)entry->data.nFileSizeHigh << 32) | entry->data.nFileSizeLow;
1925
1926                         _stprintf(buffer, _T("%") LONGLONGARG _T("d"), size);
1927
1928                         if (calcWidthCol == -1)
1929                                 output_number(pane, dis, col, buffer);
1930                         else if (calcWidthCol==col || calcWidthCol==COLUMNS)
1931                                 calc_width(pane, dis, col, buffer);/*TODO: not ever time enough */
1932                 }
1933
1934                 col++;
1935         }
1936
1937         /* display file date */
1938         if (visible_cols & (COL_DATE|COL_TIME)) {
1939 #ifndef _NO_EXTENSIONS
1940                 format_date(&entry->data.ftCreationTime, buffer, visible_cols);
1941                 if (calcWidthCol == -1)
1942                         output_text(pane, dis, col, buffer, 0);
1943                 else if (calcWidthCol==col || calcWidthCol==COLUMNS)
1944                         calc_width(pane, dis, col, buffer);
1945                 col++;
1946
1947                 format_date(&entry->data.ftLastAccessTime, buffer, visible_cols);
1948                 if (calcWidthCol == -1)
1949                         output_text(pane, dis, col, buffer, 0);
1950                 else if (calcWidthCol==col || calcWidthCol==COLUMNS)
1951                         calc_width(pane, dis, col, buffer);
1952                 col++;
1953 #endif
1954
1955                 format_date(&entry->data.ftLastWriteTime, buffer, visible_cols);
1956                 if (calcWidthCol == -1)
1957                         output_text(pane, dis, col, buffer, 0);
1958                 else if (calcWidthCol==col || calcWidthCol==COLUMNS)
1959                         calc_width(pane, dis, col, buffer);
1960                 col++;
1961         }
1962
1963 #ifndef _NO_EXTENSIONS
1964         if (entry->bhfi_valid) {
1965             ULONGLONG index = ((ULONGLONG)entry->bhfi.nFileIndexHigh << 32) | entry->bhfi.nFileIndexLow;
1966
1967                 if (visible_cols & COL_INDEX) {
1968                         _stprintf(buffer, _T("%") LONGLONGARG _T("X"), index);
1969                         if (calcWidthCol == -1)
1970                                 output_text(pane, dis, col, buffer, DT_RIGHT);
1971                         else if (calcWidthCol==col || calcWidthCol==COLUMNS)
1972                                 calc_width(pane, dis, col, buffer);
1973                         col++;
1974                 }
1975
1976                 if (visible_cols & COL_LINKS) {
1977                         wsprintf(buffer, _T("%d"), entry->bhfi.nNumberOfLinks);
1978                         if (calcWidthCol == -1)
1979                                 output_text(pane, dis, col, buffer, DT_CENTER);
1980                         else if (calcWidthCol==col || calcWidthCol==COLUMNS)
1981                                 calc_width(pane, dis, col, buffer);
1982                         col++;
1983                 }
1984         } else
1985                 col += 2;
1986 #endif
1987
1988         /* show file attributes */
1989         if (visible_cols & COL_ATTRIBUTES) {
1990 #ifdef _NO_EXTENSIONS
1991                 _tcscpy(buffer, _T(" \t \t \t \t "));
1992 #else
1993                 _tcscpy(buffer, _T(" \t \t \t \t \t \t \t \t \t \t \t "));
1994 #endif
1995
1996                 if (attrs & FILE_ATTRIBUTE_NORMAL)                                      buffer[ 0] = 'N';
1997                 else {
1998                         if (attrs & FILE_ATTRIBUTE_READONLY)                    buffer[ 2] = 'R';
1999                         if (attrs & FILE_ATTRIBUTE_HIDDEN)                              buffer[ 4] = 'H';
2000                         if (attrs & FILE_ATTRIBUTE_SYSTEM)                              buffer[ 6] = 'S';
2001                         if (attrs & FILE_ATTRIBUTE_ARCHIVE)                             buffer[ 8] = 'A';
2002                         if (attrs & FILE_ATTRIBUTE_COMPRESSED)                  buffer[10] = 'C';
2003 #ifndef _NO_EXTENSIONS
2004                         if (attrs & FILE_ATTRIBUTE_DIRECTORY)                   buffer[12] = 'D';
2005                         if (attrs & FILE_ATTRIBUTE_ENCRYPTED)                   buffer[14] = 'E';
2006                         if (attrs & FILE_ATTRIBUTE_TEMPORARY)                   buffer[16] = 'T';
2007                         if (attrs & FILE_ATTRIBUTE_SPARSE_FILE)                 buffer[18] = 'P';
2008                         if (attrs & FILE_ATTRIBUTE_REPARSE_POINT)               buffer[20] = 'Q';
2009                         if (attrs & FILE_ATTRIBUTE_OFFLINE)                             buffer[22] = 'O';
2010                         if (attrs & FILE_ATTRIBUTE_NOT_CONTENT_INDEXED) buffer[24] = 'X';
2011 #endif
2012                 }
2013
2014                 if (calcWidthCol == -1)
2015                         output_tabbed_text(pane, dis, col, buffer);
2016                 else if (calcWidthCol==col || calcWidthCol==COLUMNS)
2017                         calc_tabbed_width(pane, dis, col, buffer);
2018
2019                 col++;
2020         }
2021
2022 /*TODO
2023         if (flags.security) {
2024                 DWORD rights = get_access_mask();
2025
2026                 tcscpy(buffer, _T(" \t \t \t  \t  \t \t \t  \t  \t \t \t "));
2027
2028                 if (rights & FILE_READ_DATA)                    buffer[ 0] = 'R';
2029                 if (rights & FILE_WRITE_DATA)                   buffer[ 2] = 'W';
2030                 if (rights & FILE_APPEND_DATA)                  buffer[ 4] = 'A';
2031                 if (rights & FILE_READ_EA)                              {buffer[6] = 'entry'; buffer[ 7] = 'R';}
2032                 if (rights & FILE_WRITE_EA)                             {buffer[9] = 'entry'; buffer[10] = 'W';}
2033                 if (rights & FILE_EXECUTE)                              buffer[12] = 'X';
2034                 if (rights & FILE_DELETE_CHILD)                 buffer[14] = 'D';
2035                 if (rights & FILE_READ_ATTRIBUTES)              {buffer[16] = 'a'; buffer[17] = 'R';}
2036                 if (rights & FILE_WRITE_ATTRIBUTES)             {buffer[19] = 'a'; buffer[20] = 'W';}
2037                 if (rights & WRITE_DAC)                                 buffer[22] = 'C';
2038                 if (rights & WRITE_OWNER)                               buffer[24] = 'O';
2039                 if (rights & SYNCHRONIZE)                               buffer[26] = 'S';
2040
2041                 output_text(dis, col++, buffer, DT_LEFT, 3, psize);
2042         }
2043
2044         if (flags.description) {
2045                 get_description(buffer);
2046                 output_text(dis, col++, buffer, 0, psize);
2047         }
2048 */
2049
2050 #ifdef _NO_EXTENSIONS
2051   }
2052
2053         /* draw focus frame */
2054         if ((dis->itemState&ODS_FOCUS) && calcWidthCol==-1) {
2055                 /* Currently [04/2000] Wine neither behaves exactly the same */
2056                 /* way as WIN 95 nor like Windows NT... */
2057                 HGDIOBJ lastBrush;
2058                 HPEN lastPen;
2059                 HPEN hpen;
2060
2061                 if (!(GetVersion() & 0x80000000)) {     /* Windows NT? */
2062                         LOGBRUSH lb = {PS_SOLID, RGB(255,255,255)};
2063                         hpen = ExtCreatePen(PS_COSMETIC|PS_ALTERNATE, 1, &lb, 0, 0);
2064                 } else
2065                         hpen = CreatePen(PS_DOT, 0, RGB(255,255,255));
2066
2067                 lastPen = SelectPen(dis->hDC, hpen);
2068                 lastBrush = SelectObject(dis->hDC, GetStockObject(HOLLOW_BRUSH));
2069                 SetROP2(dis->hDC, R2_XORPEN);
2070                 Rectangle(dis->hDC, focusRect.left, focusRect.top, focusRect.right, focusRect.bottom);
2071                 SelectObject(dis->hDC, lastBrush);
2072                 SelectObject(dis->hDC, lastPen);
2073                 DeleteObject(hpen);
2074         }
2075 #endif
2076 }
2077
2078
2079 #ifdef _NO_EXTENSIONS
2080
2081 static void draw_splitbar(HWND hwnd, int x)
2082 {
2083         RECT rt;
2084         HDC hdc = GetDC(hwnd);
2085
2086         GetClientRect(hwnd, &rt);
2087
2088         rt.left = x - SPLIT_WIDTH/2;
2089         rt.right = x + SPLIT_WIDTH/2+1;
2090
2091         InvertRect(hdc, &rt);
2092
2093         ReleaseDC(hwnd, hdc);
2094 }
2095
2096 #endif
2097
2098
2099 #ifndef _NO_EXTENSIONS
2100
2101 static void set_header(Pane* pane)
2102 {
2103         HD_ITEM item;
2104         int scroll_pos = GetScrollPos(pane->hwnd, SB_HORZ);
2105         int i=0, x=0;
2106
2107         item.mask = HDI_WIDTH;
2108         item.cxy = 0;
2109
2110         for(; x+pane->widths[i]<scroll_pos && i<COLUMNS; i++) {
2111                 x += pane->widths[i];
2112                 Header_SetItem(pane->hwndHeader, i, &item);
2113         }
2114
2115         if (i < COLUMNS) {
2116                 x += pane->widths[i];
2117                 item.cxy = x - scroll_pos;
2118                 Header_SetItem(pane->hwndHeader, i++, &item);
2119
2120                 for(; i<COLUMNS; i++) {
2121                         item.cxy = pane->widths[i];
2122                         x += pane->widths[i];
2123                         Header_SetItem(pane->hwndHeader, i, &item);
2124                 }
2125         }
2126 }
2127
2128 static LRESULT pane_notify(Pane* pane, NMHDR* pnmh)
2129 {
2130         switch(pnmh->code) {
2131                 case HDN_TRACK:
2132                 case HDN_ENDTRACK: {
2133                         HD_NOTIFY* phdn = (HD_NOTIFY*) pnmh;
2134                         int idx = phdn->iItem;
2135                         int dx = phdn->pitem->cxy - pane->widths[idx];
2136                         int i;
2137
2138                         RECT clnt;
2139                         GetClientRect(pane->hwnd, &clnt);
2140
2141                         /* move immediate to simulate HDS_FULLDRAG (for now [04/2000] not realy needed with WINELIB) */
2142                         Header_SetItem(pane->hwndHeader, idx, phdn->pitem);
2143
2144                         pane->widths[idx] += dx;
2145
2146                         for(i=idx; ++i<=COLUMNS; )
2147                                 pane->positions[i] += dx;
2148
2149                         {
2150                                 int scroll_pos = GetScrollPos(pane->hwnd, SB_HORZ);
2151                                 RECT rt_scr;
2152                                 RECT rt_clip;
2153
2154                                 rt_scr.left   = pane->positions[idx+1]-scroll_pos;
2155                                 rt_scr.top    = 0;
2156                                 rt_scr.right  = clnt.right;
2157                                 rt_scr.bottom = clnt.bottom;
2158
2159                                 rt_clip.left   = pane->positions[idx]-scroll_pos;
2160                                 rt_clip.top    = 0;
2161                                 rt_clip.right  = clnt.right;
2162                                 rt_clip.bottom = clnt.bottom;
2163
2164                                 if (rt_scr.left < 0) rt_scr.left = 0;
2165                                 if (rt_clip.left < 0) rt_clip.left = 0;
2166
2167                                 ScrollWindowEx(pane->hwnd, dx, 0, &rt_scr, &rt_clip, 0, 0, SW_INVALIDATE);
2168
2169                                 rt_clip.right = pane->positions[idx+1];
2170                                 RedrawWindow(pane->hwnd, &rt_clip, 0, RDW_INVALIDATE|RDW_UPDATENOW);
2171
2172                                 if (pnmh->code == HDN_ENDTRACK) {
2173                                         ListBox_SetHorizontalExtent(pane->hwnd, pane->positions[COLUMNS]);
2174
2175                                         if (GetScrollPos(pane->hwnd, SB_HORZ) != scroll_pos)
2176                                                 set_header(pane);
2177                                 }
2178                         }
2179
2180                         return FALSE;
2181                 }
2182
2183                 case HDN_DIVIDERDBLCLICK: {
2184                         HD_NOTIFY* phdn = (HD_NOTIFY*) pnmh;
2185                         HD_ITEM item;
2186
2187                         calc_single_width(pane, phdn->iItem);
2188                         item.mask = HDI_WIDTH;
2189                         item.cxy = pane->widths[phdn->iItem];
2190
2191                         Header_SetItem(pane->hwndHeader, phdn->iItem, &item);
2192                         InvalidateRect(pane->hwnd, 0, TRUE);
2193                         break;}
2194         }
2195
2196         return 0;
2197 }
2198
2199 #endif
2200
2201
2202 static void scan_entry(ChildWnd* child, Entry* entry)
2203 {
2204         TCHAR path[MAX_PATH];
2205         int idx = ListBox_GetCurSel(child->left.hwnd);
2206         HCURSOR crsrOld = SetCursor(LoadCursor(0, IDC_WAIT));
2207
2208         /* delete sub entries in left pane */
2209         for(;;) {
2210                 LRESULT res = ListBox_GetItemData(child->left.hwnd, idx+1);
2211                 Entry* sub = (Entry*) res;
2212
2213                 if (res==LB_ERR || !sub || sub->level<=entry->level)
2214                         break;
2215
2216                 ListBox_DeleteString(child->left.hwnd, idx+1);
2217         }
2218
2219         /* empty right pane */
2220         ListBox_ResetContent(child->right.hwnd);
2221
2222         /* release memory */
2223         free_entries(entry);
2224
2225         /* read contents from disk */
2226         get_path(entry, path);
2227         read_directory(entry, path, child->sortOrder);
2228
2229         /* insert found entries in right pane */
2230         insert_entries(&child->right, entry->down, -1);
2231         calc_widths(&child->right, FALSE);
2232 #ifndef _NO_EXTENSIONS
2233         set_header(&child->right);
2234 #endif
2235
2236         child->header_wdths_ok = FALSE;
2237
2238         SetCursor(crsrOld);
2239 }
2240
2241
2242 /* expand a directory entry */
2243
2244 static BOOL expand_entry(ChildWnd* child, Entry* dir)
2245 {
2246         int idx;
2247         Entry* p;
2248
2249         if (!dir || dir->expanded || !dir->down)
2250                 return FALSE;
2251
2252         p = dir->down;
2253
2254         if (p->data.cFileName[0]=='.' && p->data.cFileName[1]=='\0' && p->next) {
2255                 p = p->next;
2256
2257                 if (p->data.cFileName[0]=='.' && p->data.cFileName[1]=='.' &&
2258                                 p->data.cFileName[2]=='\0' && p->next)
2259                         p = p->next;
2260         }
2261
2262         /* no subdirectories ? */
2263         if (!(p->data.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY))
2264                 return FALSE;
2265
2266         idx = ListBox_FindItemData(child->left.hwnd, 0, dir);
2267
2268         dir->expanded = TRUE;
2269
2270         /* insert entries in left pane */
2271         insert_entries(&child->left, p, idx);
2272
2273         if (!child->header_wdths_ok) {
2274                 if (calc_widths(&child->left, FALSE)) {
2275 #ifndef _NO_EXTENSIONS
2276                         set_header(&child->left);
2277 #endif
2278
2279                         child->header_wdths_ok = TRUE;
2280                 }
2281         }
2282
2283         return TRUE;
2284 }
2285
2286
2287 static void collapse_entry(Pane* pane, Entry* dir)
2288 {
2289         int idx = ListBox_FindItemData(pane->hwnd, 0, dir);
2290
2291         ShowWindow(pane->hwnd, SW_HIDE);
2292
2293         /* hide sub entries */
2294         for(;;) {
2295                 LRESULT res = ListBox_GetItemData(pane->hwnd, idx+1);
2296                 Entry* sub = (Entry*) res;
2297
2298                 if (res==LB_ERR || !sub || sub->level<=dir->level)
2299                         break;
2300
2301                 ListBox_DeleteString(pane->hwnd, idx+1);
2302         }
2303
2304         dir->expanded = FALSE;
2305
2306         ShowWindow(pane->hwnd, SW_SHOW);
2307 }
2308
2309
2310 static void set_curdir(ChildWnd* child, Entry* entry)
2311 {
2312         TCHAR path[MAX_PATH];
2313
2314         child->left.cur = entry;
2315         child->right.root = entry;
2316         child->right.cur = entry;
2317
2318         if (!entry->scanned)
2319                 scan_entry(child, entry);
2320         else {
2321                 ListBox_ResetContent(child->right.hwnd);
2322                 insert_entries(&child->right, entry->down, -1);
2323                 calc_widths(&child->right, FALSE);
2324 #ifndef _NO_EXTENSIONS
2325                 set_header(&child->right);
2326 #endif
2327         }
2328
2329         get_path(entry, path);
2330         lstrcpy(child->path, path);
2331         SetWindowText(child->hwnd, path);
2332         SetCurrentDirectory(path);
2333 }
2334
2335
2336 static void activate_entry(ChildWnd* child, Pane* pane)
2337 {
2338         Entry* entry = pane->cur;
2339
2340         if (!entry)
2341                 return;
2342
2343         if (entry->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
2344                 int scanned_old = entry->scanned;
2345
2346                 if (!scanned_old)
2347                         scan_entry(child, entry);
2348
2349 #ifndef _NO_EXTENSIONS
2350                 if (entry->data.cFileName[0]=='.' && entry->data.cFileName[1]=='\0')
2351                         return;
2352 #endif
2353
2354                 if (entry->data.cFileName[0]=='.' && entry->data.cFileName[1]=='.' && entry->data.cFileName[2]=='\0') {
2355                         entry = child->left.cur->up;
2356                         collapse_entry(&child->left, entry);
2357                         goto focus_entry;
2358                 } else if (entry->expanded)
2359                         collapse_entry(pane, child->left.cur);
2360                 else {
2361                         expand_entry(child, child->left.cur);
2362
2363                         if (!pane->treePane) focus_entry: {
2364                                 int idx = ListBox_FindItemData(child->left.hwnd, ListBox_GetCurSel(child->left.hwnd), entry);
2365                                 ListBox_SetCurSel(child->left.hwnd, idx);
2366                                 set_curdir(child, entry);
2367                         }
2368                 }
2369
2370                 if (!scanned_old) {
2371                         calc_widths(pane, FALSE);
2372
2373 #ifndef _NO_EXTENSIONS
2374                         set_header(pane);
2375 #endif
2376                 }
2377         } else {
2378
2379                 /*TODO: start program, open document... */
2380
2381         }
2382 }
2383
2384
2385 static BOOL pane_command(Pane* pane, UINT cmd)
2386 {
2387         switch(cmd) {
2388                 case ID_VIEW_NAME:
2389                         if (pane->visible_cols) {
2390                                 pane->visible_cols = 0;
2391                                 calc_widths(pane, TRUE);
2392 #ifndef _NO_EXTENSIONS
2393                                 set_header(pane);
2394 #endif
2395                                 InvalidateRect(pane->hwnd, 0, TRUE);
2396                                 CheckMenuItem(Globals.hMenuView, ID_VIEW_NAME, MF_BYCOMMAND|MF_CHECKED);
2397                                 CheckMenuItem(Globals.hMenuView, ID_VIEW_ALL_ATTRIBUTES, MF_BYCOMMAND);
2398                                 CheckMenuItem(Globals.hMenuView, ID_VIEW_SELECTED_ATTRIBUTES, MF_BYCOMMAND);
2399                         }
2400                         break;
2401
2402                 case ID_VIEW_ALL_ATTRIBUTES:
2403                         if (pane->visible_cols != COL_ALL) {
2404                                 pane->visible_cols = COL_ALL;
2405                                 calc_widths(pane, TRUE);
2406 #ifndef _NO_EXTENSIONS
2407                                 set_header(pane);
2408 #endif
2409                                 InvalidateRect(pane->hwnd, 0, TRUE);
2410                                 CheckMenuItem(Globals.hMenuView, ID_VIEW_NAME, MF_BYCOMMAND);
2411                                 CheckMenuItem(Globals.hMenuView, ID_VIEW_ALL_ATTRIBUTES, MF_BYCOMMAND|MF_CHECKED);
2412                                 CheckMenuItem(Globals.hMenuView, ID_VIEW_SELECTED_ATTRIBUTES, MF_BYCOMMAND);
2413                         }
2414                         break;
2415
2416 #ifndef _NO_EXTENSIONS
2417                 case ID_PREFERED_SIZES: {
2418                         calc_widths(pane, TRUE);
2419                         set_header(pane);
2420                         InvalidateRect(pane->hwnd, 0, TRUE);
2421                         break;}
2422 #endif
2423
2424                         /* TODO: more command ids... */
2425
2426                 default:
2427                         return FALSE;
2428         }
2429
2430         return TRUE;
2431 }
2432
2433
2434 LRESULT CALLBACK ChildWndProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam)
2435 {
2436         static int last_split;
2437
2438         ChildWnd* child = (ChildWnd*) GetWindowLong(hwnd, GWL_USERDATA);
2439         ASSERT(child);
2440
2441         switch(nmsg) {
2442                 case WM_DRAWITEM: {
2443                         LPDRAWITEMSTRUCT dis = (LPDRAWITEMSTRUCT)lparam;
2444                         Entry* entry = (Entry*) dis->itemData;
2445
2446                         if (dis->CtlID == IDW_TREE_LEFT)
2447                                 draw_item(&child->left, dis, entry, -1);
2448                         else
2449                                 draw_item(&child->right, dis, entry, -1);
2450
2451                         return TRUE;}
2452
2453                 case WM_CREATE:
2454                         InitChildWindow(child);
2455                         break;
2456
2457                 case WM_NCDESTROY:
2458                         free_child_window(child);
2459                         SetWindowLong(hwnd, GWL_USERDATA, 0);
2460                         break;
2461
2462                 case WM_PAINT: {
2463                         PAINTSTRUCT ps;
2464                         HBRUSH lastBrush;
2465                         RECT rt;
2466                         GetClientRect(hwnd, &rt);
2467                         BeginPaint(hwnd, &ps);
2468                         rt.left = child->split_pos-SPLIT_WIDTH/2;
2469                         rt.right = child->split_pos+SPLIT_WIDTH/2+1;
2470                         lastBrush = SelectBrush(ps.hdc, (HBRUSH)GetStockObject(COLOR_SPLITBAR));
2471                         Rectangle(ps.hdc, rt.left, rt.top-1, rt.right, rt.bottom+1);
2472                         SelectObject(ps.hdc, lastBrush);
2473 #ifdef _NO_EXTENSIONS
2474                         rt.top = rt.bottom - GetSystemMetrics(SM_CYHSCROLL);
2475                         FillRect(ps.hdc, &rt, GetStockObject(BLACK_BRUSH));
2476 #endif
2477                         EndPaint(hwnd, &ps);
2478                         break;}
2479
2480                 case WM_SETCURSOR:
2481                         if (LOWORD(lparam) == HTCLIENT) {
2482                                 POINT pt;
2483                                 GetCursorPos(&pt);
2484                                 ScreenToClient(hwnd, &pt);
2485
2486                                 if (pt.x>=child->split_pos-SPLIT_WIDTH/2 && pt.x<child->split_pos+SPLIT_WIDTH/2+1) {
2487                                         SetCursor(LoadCursor(0, IDC_SIZEWE));
2488                                         return TRUE;
2489                                 }
2490                         }
2491                         goto def;
2492
2493                 case WM_LBUTTONDOWN: {
2494                         RECT rt;
2495                         int x = LOWORD(lparam);
2496
2497                         GetClientRect(hwnd, &rt);
2498
2499                         if (x>=child->split_pos-SPLIT_WIDTH/2 && x<child->split_pos+SPLIT_WIDTH/2+1) {
2500                                 last_split = child->split_pos;
2501 #ifdef _NO_EXTENSIONS
2502                                 draw_splitbar(hwnd, last_split);
2503 #endif
2504                                 SetCapture(hwnd);
2505                         }
2506
2507                         break;}
2508
2509                 case WM_LBUTTONUP:
2510                         if (GetCapture() == hwnd) {
2511 #ifdef _NO_EXTENSIONS
2512                                 RECT rt;
2513                                 int x = LOWORD(lparam);
2514                                 draw_splitbar(hwnd, last_split);
2515                                 last_split = -1;
2516                                 GetClientRect(hwnd, &rt);
2517                                 child->split_pos = x;
2518                                 resize_tree(child, rt.right, rt.bottom);
2519 #endif
2520                                 ReleaseCapture();
2521                         }
2522                         break;
2523
2524 #ifdef _NO_EXTENSIONS
2525                 case WM_CAPTURECHANGED:
2526                         if (GetCapture()==hwnd && last_split>=0)
2527                                 draw_splitbar(hwnd, last_split);
2528                         break;
2529 #endif
2530
2531                 case WM_KEYDOWN:
2532                         if (wparam == VK_ESCAPE)
2533                                 if (GetCapture() == hwnd) {
2534                                         RECT rt;
2535 #ifdef _NO_EXTENSIONS
2536                                         draw_splitbar(hwnd, last_split);
2537 #else
2538                                         child->split_pos = last_split;
2539 #endif
2540                                         GetClientRect(hwnd, &rt);
2541                                         resize_tree(child, rt.right, rt.bottom);
2542                                         last_split = -1;
2543                                         ReleaseCapture();
2544                                         SetCursor(LoadCursor(0, IDC_ARROW));
2545                                 }
2546                         break;
2547
2548                 case WM_MOUSEMOVE:
2549                         if (GetCapture() == hwnd) {
2550                                 RECT rt;
2551                                 int x = LOWORD(lparam);
2552
2553 #ifdef _NO_EXTENSIONS
2554                                 HDC hdc = GetDC(hwnd);
2555                                 GetClientRect(hwnd, &rt);
2556
2557                                 rt.left = last_split-SPLIT_WIDTH/2;
2558                                 rt.right = last_split+SPLIT_WIDTH/2+1;
2559                                 InvertRect(hdc, &rt);
2560
2561                                 last_split = x;
2562                                 rt.left = x-SPLIT_WIDTH/2;
2563                                 rt.right = x+SPLIT_WIDTH/2+1;
2564                                 InvertRect(hdc, &rt);
2565
2566                                 ReleaseDC(hwnd, hdc);
2567 #else
2568                                 GetClientRect(hwnd, &rt);
2569
2570                                 if (x>=0 && x<rt.right) {
2571                                         child->split_pos = x;
2572                                         resize_tree(child, rt.right, rt.bottom);
2573                                         rt.left = x-SPLIT_WIDTH/2;
2574                                         rt.right = x+SPLIT_WIDTH/2+1;
2575                                         InvalidateRect(hwnd, &rt, FALSE);
2576                                         UpdateWindow(child->left.hwnd);
2577                                         UpdateWindow(hwnd);
2578                                         UpdateWindow(child->right.hwnd);
2579                                 }
2580 #endif
2581                         }
2582                         break;
2583
2584 #ifndef _NO_EXTENSIONS
2585                 case WM_GETMINMAXINFO:
2586                         DefMDIChildProc(hwnd, nmsg, wparam, lparam);
2587
2588                         {LPMINMAXINFO lpmmi = (LPMINMAXINFO)lparam;
2589
2590                         lpmmi->ptMaxTrackSize.x <<= 1;/*2*GetSystemMetrics(SM_CXSCREEN) / SM_CXVIRTUALSCREEN */
2591                         lpmmi->ptMaxTrackSize.y <<= 1;/*2*GetSystemMetrics(SM_CYSCREEN) / SM_CYVIRTUALSCREEN */
2592                         break;}
2593 #endif
2594
2595                 case WM_SETFOCUS:
2596                         SetCurrentDirectory(child->path);
2597                         SetFocus(child->focus_pane? child->right.hwnd: child->left.hwnd);
2598                         break;
2599
2600                 case WM_DISPATCH_COMMAND: {
2601                         Pane* pane = GetFocus()==child->left.hwnd? &child->left: &child->right;
2602
2603                         switch(LOWORD(wparam)) {
2604                                 case ID_WINDOW_NEW: {
2605                                         ChildWnd* new_child = alloc_child_window(child->path);
2606
2607                                         if (!create_child_window(new_child))
2608                                                 free(new_child);
2609
2610                                         break;}
2611
2612                                 case ID_REFRESH:
2613                                         scan_entry(child, pane->cur);
2614                                         break;
2615
2616                                 case ID_ACTIVATE:
2617                                         activate_entry(child, pane);
2618                                         break;
2619
2620                                 default:
2621                                         return pane_command(pane, LOWORD(wparam));
2622                         }
2623
2624                         return TRUE;}
2625
2626                 case WM_COMMAND: {
2627                         Pane* pane = GetFocus()==child->left.hwnd? &child->left: &child->right;
2628
2629                         switch(HIWORD(wparam)) {
2630                                 case LBN_SELCHANGE: {
2631                                         int idx = ListBox_GetCurSel(pane->hwnd);
2632                                         Entry* entry = (Entry*) ListBox_GetItemData(pane->hwnd, idx);
2633
2634                                         if (pane == &child->left)
2635                                                 set_curdir(child, entry);
2636                                         else
2637                                                 pane->cur = entry;
2638                                         break;}
2639
2640                                 case LBN_DBLCLK:
2641                                         activate_entry(child, pane);
2642                                         break;
2643                         }
2644                         break;}
2645
2646 #ifndef _NO_EXTENSIONS
2647                 case WM_NOTIFY: {
2648                         NMHDR* pnmh = (NMHDR*) lparam;
2649                         return pane_notify(pnmh->idFrom==IDW_HEADER_LEFT? &child->left: &child->right, pnmh);}
2650 #endif
2651
2652                 case WM_SIZE:
2653                         if (wparam != SIZE_MINIMIZED)
2654                                 resize_tree(child, LOWORD(lparam), HIWORD(lparam));
2655                         /* fall through */
2656
2657                 default: def:
2658                         return DefMDIChildProc(hwnd, nmsg, wparam, lparam);
2659         }
2660
2661         return 0;
2662 }
2663
2664
2665 LRESULT CALLBACK TreeWndProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam)
2666 {
2667         ChildWnd* child = (ChildWnd*) GetWindowLong(GetParent(hwnd), GWL_USERDATA);
2668         Pane* pane = (Pane*) GetWindowLong(hwnd, GWL_USERDATA);
2669         ASSERT(child);
2670
2671         switch(nmsg) {
2672 #ifndef _NO_EXTENSIONS
2673                 case WM_HSCROLL:
2674                         set_header(pane);
2675                         break;
2676 #endif
2677
2678                 case WM_SETFOCUS:
2679                         child->focus_pane = pane==&child->right? 1: 0;
2680                         ListBox_SetSel(hwnd, TRUE, 1);
2681                         /*TODO: check menu items */
2682                         break;
2683
2684                 case WM_KEYDOWN:
2685                         if (wparam == VK_TAB) {
2686                                 /*TODO: SetFocus(Globals.hdrivebar) */
2687                                 SetFocus(child->focus_pane? child->left.hwnd: child->right.hwnd);
2688                         }
2689         }
2690
2691         return CallWindowProc(g_orgTreeWndProc, hwnd, nmsg, wparam, lparam);
2692 }
2693
2694
2695 static void InitInstance(HINSTANCE hinstance)
2696 {
2697         WNDCLASSEX wcFrame;
2698         ATOM hframeClass;
2699         WNDCLASS wcChild;
2700         WINE_UNUSED ATOM hChildClass;
2701         HMENU hMenuFrame = LoadMenu(hinstance, MAKEINTRESOURCE(IDM_WINEFILE));
2702         HMENU hMenuWindow = GetSubMenu(hMenuFrame, GetMenuItemCount(hMenuFrame)-2);
2703
2704         CLIENTCREATESTRUCT ccs;
2705
2706         INITCOMMONCONTROLSEX icc = {
2707                 sizeof(INITCOMMONCONTROLSEX),
2708                 ICC_BAR_CLASSES
2709         };
2710
2711         ChildWnd* child;
2712         TCHAR path[MAX_PATH];
2713
2714         HDC hdc = GetDC(0);
2715
2716
2717         wcFrame.cbSize        = sizeof(WNDCLASSEX);
2718         wcFrame.style         = 0;
2719         wcFrame.lpfnWndProc   = FrameWndProc;
2720         wcFrame.cbClsExtra    = 0;
2721         wcFrame.cbWndExtra    = 0;
2722         wcFrame.hInstance     = hinstance;
2723         wcFrame.hIcon         = LoadIcon(hinstance,
2724                                          MAKEINTRESOURCE(IDI_WINEFILE));
2725         wcFrame.hCursor       = LoadCursor(0, IDC_ARROW);
2726         wcFrame.hbrBackground = 0;
2727         wcFrame.lpszMenuName  = 0;
2728         wcFrame.lpszClassName = WINEFILEFRAME;
2729         wcFrame.hIconSm       = (HICON)LoadImage(hinstance,
2730                                                  MAKEINTRESOURCE(IDI_WINEFILE),
2731                                                  IMAGE_ICON,
2732                                                  GetSystemMetrics(SM_CXSMICON),
2733                                                  GetSystemMetrics(SM_CYSMICON),
2734                                                  LR_SHARED);
2735
2736         /* register frame window class */
2737         hframeClass = RegisterClassEx(&wcFrame);
2738
2739         wcChild.style         = CS_CLASSDC|CS_DBLCLKS|CS_VREDRAW;
2740         wcChild.lpfnWndProc   = ChildWndProc;
2741         wcChild.cbClsExtra    = 0;
2742         wcChild.cbWndExtra    = 0;
2743         wcChild.hInstance     = hinstance;
2744         wcChild.hIcon         = 0;
2745         wcChild.hCursor       = LoadCursor(0, IDC_ARROW);
2746         wcChild.hbrBackground = 0;
2747         wcChild.lpszMenuName  = 0;
2748         wcChild.lpszClassName = WINEFILETREE;
2749
2750         /* register tree windows class */
2751         hChildClass = RegisterClass(&wcChild);
2752
2753         ccs.hWindowMenu  = hMenuWindow;
2754         ccs.idFirstChild = IDW_FIRST_CHILD;
2755
2756         Globals.hMenuFrame = hMenuFrame;
2757         Globals.hMenuView = GetSubMenu(hMenuFrame, 3);
2758         Globals.hMenuOptions = GetSubMenu(hMenuFrame, 4);
2759
2760         Globals.haccel = LoadAccelerators(hinstance, MAKEINTRESOURCE(IDA_WINEFILE));
2761
2762         Globals.hfont = CreateFont(-MulDiv(8,GetDeviceCaps(hdc,LOGPIXELSY),72), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, _T("MS Sans Serif"));
2763
2764         ReleaseDC(0, hdc);
2765
2766         Globals.hInstance = hinstance;
2767
2768         /* create main window */
2769         Globals.hMainWnd = CreateWindowEx(0, (LPCTSTR)(int)hframeClass, _T("Wine File"), WS_OVERLAPPEDWINDOW,
2770                                         CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
2771                                         0/*hWndParent*/, Globals.hMenuFrame, hinstance, 0/*lpParam*/);
2772
2773
2774         Globals.hmdiclient = CreateWindowEx(0, _T("MDICLIENT"), NULL,
2775                                         WS_CHILD|WS_CLIPCHILDREN|WS_VSCROLL|WS_HSCROLL|WS_VISIBLE|WS_BORDER,
2776                                         0, 0, 0, 0,
2777                                         Globals.hMainWnd, 0, hinstance, &ccs);
2778
2779
2780         InitCommonControlsEx(&icc);
2781
2782         {
2783                 TBBUTTON drivebarBtn = {0, 0, TBSTATE_ENABLED, TBSTYLE_SEP};
2784                 int btn = 1;
2785                 PTSTR p;
2786
2787                 Globals.hdrivebar = CreateToolbarEx(Globals.hMainWnd, WS_CHILD|WS_VISIBLE|CCS_NOMOVEY|TBSTYLE_LIST,
2788                                         IDW_DRIVEBAR, 2, Globals.hInstance, IDB_DRIVEBAR, &drivebarBtn,
2789                                         1, 16, 13, 16, 13, sizeof(TBBUTTON));
2790                 CheckMenuItem(Globals.hMenuOptions, ID_VIEW_DRIVE_BAR, MF_BYCOMMAND|MF_CHECKED);
2791
2792                 GetLogicalDriveStrings(BUFFER_LEN, Globals.drives);
2793
2794                 drivebarBtn.fsStyle = TBSTYLE_BUTTON;
2795
2796 #ifndef _NO_EXTENSIONS
2797 #ifdef __linux__
2798                 /* insert unix file system button */
2799                 SendMessage(Globals.hdrivebar, TB_ADDSTRING, 0, (LPARAM)_T("/\0"));
2800
2801                 drivebarBtn.idCommand = ID_DRIVE_UNIX_FS;
2802                 SendMessage(Globals.hdrivebar, TB_INSERTBUTTON, btn++, (LPARAM)&drivebarBtn);
2803                 drivebarBtn.iString++;
2804 #endif
2805
2806                 /* register windows drive root strings */
2807                 SendMessage(Globals.hdrivebar, TB_ADDSTRING, 0, (LPARAM)Globals.drives);
2808 #endif
2809
2810                 drivebarBtn.idCommand = ID_DRIVE_FIRST;
2811
2812                 for(p=Globals.drives; *p; ) {
2813 #ifdef _NO_EXTENSIONS
2814                   /* insert drive letter */
2815                         TCHAR b[3] = {tolower(*p)};
2816                         SendMessage(Globals.hdrivebar, TB_ADDSTRING, 0, (LPARAM)b);
2817 #endif
2818                         switch(GetDriveType(p)) {
2819                                 case DRIVE_REMOVABLE:   drivebarBtn.iBitmap = 1;        break;
2820                                 case DRIVE_CDROM:               drivebarBtn.iBitmap = 3;        break;
2821                                 case DRIVE_REMOTE:              drivebarBtn.iBitmap = 4;        break;
2822                                 case DRIVE_RAMDISK:             drivebarBtn.iBitmap = 5;        break;
2823                                 default:/*DRIVE_FIXED*/ drivebarBtn.iBitmap = 2;
2824                         }
2825
2826                         SendMessage(Globals.hdrivebar, TB_INSERTBUTTON, btn++, (LPARAM)&drivebarBtn);
2827                         drivebarBtn.idCommand++;
2828                         drivebarBtn.iString++;
2829
2830                         while(*p++);
2831                 }
2832         }
2833
2834         {
2835                 TBBUTTON toolbarBtns[] = {
2836                         {0, 0, 0, TBSTYLE_SEP},
2837                         {0, ID_WINDOW_NEW, TBSTATE_ENABLED, TBSTYLE_BUTTON},
2838                         {1, ID_WINDOW_CASCADE, TBSTATE_ENABLED, TBSTYLE_BUTTON},
2839                         {2, ID_WINDOW_TILE_HORZ, TBSTATE_ENABLED, TBSTYLE_BUTTON},
2840                         {3, ID_WINDOW_TILE_VERT, TBSTATE_ENABLED, TBSTYLE_BUTTON},
2841                         {4, 2/*TODO: ID_...*/, TBSTATE_ENABLED, TBSTYLE_BUTTON},
2842                         {5, 2/*TODO: ID_...*/, TBSTATE_ENABLED, TBSTYLE_BUTTON},
2843                 };
2844
2845                 Globals.htoolbar = CreateToolbarEx(Globals.hMainWnd, WS_CHILD|WS_VISIBLE,
2846                         IDW_TOOLBAR, 2, Globals.hInstance, IDB_TOOLBAR, toolbarBtns,
2847                         sizeof(toolbarBtns)/sizeof(TBBUTTON), 16, 15, 16, 15, sizeof(TBBUTTON));
2848                 CheckMenuItem(Globals.hMenuOptions, ID_VIEW_TOOL_BAR, MF_BYCOMMAND|MF_CHECKED);
2849         }
2850
2851         Globals.hstatusbar = CreateStatusWindow(WS_CHILD|WS_VISIBLE, 0, Globals.hMainWnd, IDW_STATUSBAR);
2852         CheckMenuItem(Globals.hMenuOptions, ID_VIEW_STATUSBAR, MF_BYCOMMAND|MF_CHECKED);
2853
2854 /* CreateStatusWindow does not accept WS_BORDER
2855         Globals.hstatusbar = CreateWindowEx(WS_EX_NOPARENTNOTIFY, STATUSCLASSNAME, 0,
2856                                         WS_CHILD|WS_VISIBLE|WS_CLIPSIBLINGS|WS_BORDER|CCS_NODIVIDER, 0,0,0,0,
2857                                         Globals.hMainWnd, (HMENU)IDW_STATUSBAR, hinstance, 0);*/
2858
2859         /*TODO: read paths and window placements from registry */
2860         GetCurrentDirectory(MAX_PATH, path);
2861         child = alloc_child_window(path);
2862
2863         child->pos.showCmd = SW_SHOWMAXIMIZED;
2864         child->pos.rcNormalPosition.left = 0;
2865         child->pos.rcNormalPosition.top = 0;
2866         child->pos.rcNormalPosition.right = 320;
2867         child->pos.rcNormalPosition.bottom = 280;
2868
2869         if (!create_child_window(child))
2870                 free(child);
2871
2872         SetWindowPlacement(child->hwnd, &child->pos);
2873
2874         Globals.himl = ImageList_LoadBitmap(Globals.hInstance, MAKEINTRESOURCE(IDB_IMAGES), 16, 0, RGB(0,255,0));
2875
2876         Globals.prescan_node = FALSE;
2877 }
2878
2879 void ExitInstance()
2880 {
2881         ImageList_Destroy(Globals.himl);
2882 }
2883
2884
2885 #ifdef _NO_EXTENSIONS
2886
2887 /* search for already running win[e]files */
2888
2889 static int g_foundPrevInstance = 0;
2890
2891 static BOOL CALLBACK EnumWndProc(HWND hwnd, LPARAM lparam)
2892 {
2893         TCHAR cls[128];
2894
2895         GetClassName(hwnd, cls, 128);
2896
2897         if (!lstrcmp(cls, (LPCTSTR)lparam)) {
2898                 g_foundPrevInstance++;
2899                 return FALSE;
2900         }
2901
2902         return TRUE;
2903 }
2904
2905 #endif
2906
2907
2908 int APIENTRY WinMain(HINSTANCE hinstance,
2909                                          HINSTANCE previnstance,
2910                                          LPSTR     cmdline,
2911                                          int       cmdshow)
2912 {
2913         MSG msg;
2914
2915 #ifdef _NO_EXTENSIONS
2916         /* allow only one running instance */
2917         EnumWindows(EnumWndProc, (LPARAM)WINEFILEFRAME);
2918
2919         if (g_foundPrevInstance)
2920                 return 1;
2921 #endif
2922
2923         InitInstance(hinstance);
2924
2925         if (cmdshow == SW_SHOWNORMAL) {
2926                 /*TODO: read window placement from registry */
2927                 cmdshow = SW_MAXIMIZE;
2928         }
2929
2930         ShowWindow(Globals.hMainWnd, cmdshow);
2931         UpdateWindow(Globals.hMainWnd);
2932
2933         while(GetMessage(&msg, 0, 0, 0)) {
2934                 if (!TranslateMDISysAccel(Globals.hmdiclient, &msg) &&
2935                         !TranslateAccelerator(Globals.hMainWnd, Globals.haccel, &msg))
2936                 {
2937                         TranslateMessage(&msg);
2938                         DispatchMessage(&msg);
2939                 }
2940         }
2941
2942         ExitInstance();
2943
2944         return 0;
2945 }