winefile.c 120 KB
Newer Older
1 2 3
/*
 * Winefile
 *
4
 * Copyright 2000, 2003, 2004, 2005 Martin Fuchs
5
 * Copyright 2006 Jason Green
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 21
 */

22
#ifdef __WINE__
23 24
#include "config.h"
#include "wine/port.h"
25 26 27 28 29

/* for unix filesystem function calls */
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
30
#endif
31

32 33
#define COBJMACROS

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#include "winefile.h"
#include "resource.h"

#ifdef _NO_EXTENSIONS
#undef _LEFT_FILES
#endif

#ifndef _MAX_PATH
#define _MAX_DRIVE          3
#define _MAX_FNAME          256
#define _MAX_DIR            _MAX_FNAME
#define _MAX_EXT            _MAX_FNAME
#define _MAX_PATH           260
#endif

49
#ifdef NONAMELESSUNION
Francois Gouget's avatar
Francois Gouget committed
50
#define	UNION_MEMBER(x) DUMMYUNIONNAME.x
51 52 53 54 55 56
#else
#define	UNION_MEMBER(x) x
#endif


#ifdef _SHELL_FOLDERS
57
#define	DEFAULT_SPLIT_POS	300
58
#else
59
#define	DEFAULT_SPLIT_POS	200
60 61
#endif

62 63 64 65 66 67 68
static const WCHAR registry_key[] = { 'S','o','f','t','w','a','r','e','\\',
                                      'W','i','n','e','\\',
                                      'W','i','n','e','F','i','l','e','\0'};
static const WCHAR reg_start_x[] = { 's','t','a','r','t','X','\0'};
static const WCHAR reg_start_y[] = { 's','t','a','r','t','Y','\0'};
static const WCHAR reg_width[] = { 'w','i','d','t','h','\0'};
static const WCHAR reg_height[] = { 'h','e','i','g','h','t','\0'};
69 70 71 72 73 74 75 76

enum ENTRY_TYPE {
	ET_WINDOWS,
	ET_UNIX,
#ifdef _SHELL_FOLDERS
	ET_SHELL
#endif
};
77 78 79 80 81 82

typedef struct _Entry {
	struct _Entry*	next;
	struct _Entry*	down;
	struct _Entry*	up;

83 84 85
	BOOL			expanded;
	BOOL			scanned;
	int				level;
86 87 88 89 90

	WIN32_FIND_DATA	data;

#ifndef _NO_EXTENSIONS
	BY_HANDLE_FILE_INFORMATION bhfi;
91 92 93 94 95 96 97
	BOOL			bhfi_valid;
	enum ENTRY_TYPE	etype;
#endif
#ifdef _SHELL_FOLDERS
	LPITEMIDLIST	pidl;
	IShellFolder*	folder;
	HICON			hicon;
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
#endif
} Entry;

typedef struct {
	Entry	entry;
	TCHAR	path[MAX_PATH];
	TCHAR	volname[_MAX_FNAME];
	TCHAR	fs[_MAX_DIR];
	DWORD	drive_type;
	DWORD	fs_flags;
} Root;

enum COLUMN_FLAGS {
	COL_SIZE		= 0x01,
	COL_DATE		= 0x02,
	COL_TIME		= 0x04,
	COL_ATTRIBUTES	= 0x08,
	COL_DOSNAMES	= 0x10,
#ifdef _NO_EXTENSIONS
	COL_ALL = COL_SIZE|COL_DATE|COL_TIME|COL_ATTRIBUTES|COL_DOSNAMES
#else
	COL_INDEX		= 0x20,
	COL_LINKS		= 0x40,
	COL_ALL = COL_SIZE|COL_DATE|COL_TIME|COL_ATTRIBUTES|COL_DOSNAMES|COL_INDEX|COL_LINKS
#endif
};

typedef enum {
	SORT_NAME,
	SORT_EXT,
	SORT_SIZE,
	SORT_DATE
} SORT_ORDER;

typedef struct {
	HWND	hwnd;
#ifndef _NO_EXTENSIONS
	HWND	hwndHeader;
#endif

#ifndef _NO_EXTENSIONS
#define	COLUMNS	10
#else
#define	COLUMNS	5
#endif
	int		widths[COLUMNS];
	int		positions[COLUMNS+1];

	BOOL	treePane;
	int		visible_cols;
	Entry*	root;
	Entry*	cur;
} Pane;

typedef struct {
	HWND	hwnd;
	Pane	left;
	Pane	right;
156
	int		focus_pane;		/* 0: left  1: right */
157 158 159 160 161
	WINDOWPLACEMENT pos;
	int		split_pos;
	BOOL	header_wdths_ok;

	TCHAR	path[MAX_PATH];
162 163
	TCHAR	filter_pattern[MAX_PATH];
	int		filter_flags;
164 165 166 167 168 169
	Root	root;

	SORT_ORDER sortOrder;
} ChildWnd;


170

171
static void read_directory(Entry* dir, LPCTSTR path, SORT_ORDER sortOrder, HWND hwnd);
172 173
static void set_curdir(ChildWnd* child, Entry* entry, int idx, HWND hwnd);
static void refresh_child(ChildWnd* child);
174
static void refresh_drives(void);
175
static void get_path(Entry* dir, PTSTR path);
176
static void format_date(const FILETIME* ft, TCHAR* buffer, int visible_cols);
177

Mike McCormack's avatar
Mike McCormack committed
178 179 180
static LRESULT CALLBACK FrameWndProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam);
static LRESULT CALLBACK ChildWndProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam);
static LRESULT CALLBACK TreeWndProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam);
181 182


183 184 185
/* globals */
WINEFILE_GLOBALS Globals;

186 187
static int last_split;

188
/* some common string constants */
189
static const TCHAR sEmpty[] = {'\0'};
190
static const WCHAR sSpace[] = {' ', '\0'};
191 192
static const TCHAR sNumFmt[] = {'%','d','\0'};
static const TCHAR sQMarks[] = {'?','?','?','\0'};
193 194

/* window class names */
195 196
static const TCHAR sWINEFILEFRAME[] = {'W','F','S','_','F','r','a','m','e','\0'};
static const TCHAR sWINEFILETREE[] = {'W','F','S','_','T','r','e','e','\0'};
197 198 199

#ifdef _MSC_VER
/* #define LONGLONGARG _T("I64") */
200 201
static const TCHAR sLongHexFmt[] = {'%','I','6','4','X','\0'};
static const TCHAR sLongNumFmt[] = {'%','I','6','4','d','\0'};
202 203
#else
/* #define LONGLONGARG _T("L") */
204 205
static const TCHAR sLongHexFmt[] = {'%','L','X','\0'};
static const TCHAR sLongNumFmt[] = {'%','L','d','\0'};
206 207 208
#endif


209 210 211
/* load resource string */
static LPTSTR load_string(LPTSTR buffer, UINT id)
{
212
	LoadString(Globals.hInstance, id, buffer, BUFFER_LEN);
213

214
	return buffer;
215 216 217 218 219
}

#define RS(b, i) load_string(b, i)


220
/* display error message for the specified WIN32 error code */
221 222
static void display_error(HWND hwnd, DWORD error)
{
223
	TCHAR b1[BUFFER_LEN], b2[BUFFER_LEN];
224 225 226 227
	PTSTR msg;

	if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
		0, error, MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), (PTSTR)&msg, 0, NULL))
228
		MessageBox(hwnd, msg, RS(b2,IDS_WINEFILE), MB_OK);
229
	else
230
		MessageBox(hwnd, RS(b1,IDS_ERROR), RS(b2,IDS_WINEFILE), MB_OK);
231 232 233 234

	LocalFree(msg);
}

235 236 237 238 239 240 241 242 243 244 245

/* display network error message using WNetGetLastError() */
static void display_network_error(HWND hwnd)
{
	TCHAR msg[BUFFER_LEN], provider[BUFFER_LEN], b2[BUFFER_LEN];
	DWORD error;

	if (WNetGetLastError(&error, msg, BUFFER_LEN, provider, BUFFER_LEN) == NO_ERROR)
		MessageBox(hwnd, msg, RS(b2,IDS_WINEFILE), MB_OK);
}

246 247
static VOID WineLicense(HWND Wnd)
{
248 249 250 251
	WCHAR cap[20], text[1024];
	LoadStringW(Globals.hInstance, IDS_LICENSE, text, 1024);
	LoadStringW(Globals.hInstance, IDS_LICENSE_CAPTION, cap, 20);
	MessageBoxW(Wnd, text, cap, MB_ICONINFORMATION | MB_OK);
252 253 254 255
}

static VOID WineWarranty(HWND Wnd)
{
256 257 258 259
	WCHAR cap[20], text[1024];
	LoadStringW(Globals.hInstance, IDS_WARRANTY, text, 1024);
	LoadStringW(Globals.hInstance, IDS_WARRANTY_CAPTION, cap, 20);
	MessageBoxW(Wnd, text, cap, MB_ICONEXCLAMATION | MB_OK);
260 261
}

262 263
static inline BOOL get_check(HWND hwnd, INT id)
{
264
	return BST_CHECKED&SendMessageW(GetDlgItem(hwnd, id), BM_GETSTATE, 0, 0);
265 266 267 268
}

static inline INT set_check(HWND hwnd, INT id, BOOL on)
{
269
	return SendMessageW(GetDlgItem(hwnd, id), BM_SETCHECK, on?BST_CHECKED:BST_UNCHECKED, 0);
270
}
271

272 273
static inline void choose_font(HWND hwnd)
{
274 275 276
        WCHAR dlg_name[BUFFER_LEN], dlg_info[BUFFER_LEN];
        CHOOSEFONTW chFont;
        LOGFONTW lFont;
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293

        HDC hdc = GetDC(hwnd);
        chFont.lStructSize = sizeof(CHOOSEFONT);
        chFont.hwndOwner = hwnd;
        chFont.hDC = NULL;
        chFont.lpLogFont = &lFont;
        chFont.Flags = CF_SCREENFONTS | CF_FORCEFONTEXIST | CF_LIMITSIZE | CF_NOSCRIPTSEL;
        chFont.rgbColors = RGB(0,0,0);
        chFont.lCustData = 0;
        chFont.lpfnHook = NULL;
        chFont.lpTemplateName = NULL;
        chFont.hInstance = Globals.hInstance;
        chFont.lpszStyle = NULL;
        chFont.nFontType = SIMULATED_FONTTYPE;
        chFont.nSizeMin = 0;
        chFont.nSizeMax = 24;

294
        if (ChooseFontW(&chFont)) {
295 296 297 298
                HWND childWnd;
                HFONT hFontOld;

                DeleteObject(Globals.hfont);
299
                Globals.hfont = CreateFontIndirectW(&lFont);
300
                hFontOld = SelectObject(hdc, Globals.hfont);
301
                GetTextExtentPoint32W(hdc, sSpace, 1, &Globals.spaceSize);
302 303 304

                /* change font in all open child windows */
                for(childWnd=GetWindow(Globals.hmdiclient,GW_CHILD); childWnd; childWnd=GetNextWindow(childWnd,GW_HWNDNEXT)) {
305 306 307 308 309
                        ChildWnd* child = (ChildWnd*) GetWindowLongPtrW(childWnd, GWLP_USERDATA);
                        SendMessageW(child->left.hwnd, WM_SETFONT, (WPARAM)Globals.hfont, TRUE);
                        SendMessageW(child->right.hwnd, WM_SETFONT, (WPARAM)Globals.hfont, TRUE);
                        SendMessageW(child->left.hwnd, LB_SETITEMHEIGHT, 1, max(Globals.spaceSize.cy,IMAGE_HEIGHT+3));
                        SendMessageW(child->right.hwnd, LB_SETITEMHEIGHT, 1, max(Globals.spaceSize.cy,IMAGE_HEIGHT+3));
310 311 312 313 314 315 316
                        InvalidateRect(child->left.hwnd, NULL, TRUE);
                        InvalidateRect(child->right.hwnd, NULL, TRUE);
                }

                SelectObject(hdc, hFontOld);
        }
        else if (CommDlgExtendedError()) {
317 318 319
                LoadStringW(Globals.hInstance, IDS_FONT_SEL_DLG_NAME, dlg_name, BUFFER_LEN);
                LoadStringW(Globals.hInstance, IDS_FONT_SEL_ERROR, dlg_info, BUFFER_LEN);
                MessageBoxW(hwnd, dlg_info, dlg_name, MB_OK);
320 321 322 323 324
        }

        ReleaseDC(hwnd, hdc);
}

325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
#ifdef __WINE__

#ifdef UNICODE

/* call vswprintf() in msvcrt.dll */
/*TODO: fix swprintf() in non-msvcrt mode, so that this dynamic linking function can be removed */
static int msvcrt_swprintf(WCHAR* buffer, const WCHAR* fmt, ...)
{
	static int (__cdecl *pvswprintf)(WCHAR*, const WCHAR*, va_list) = NULL;
	va_list ap;
	int ret;

	if (!pvswprintf) {
		HMODULE hModMsvcrt = LoadLibraryA("msvcrt");
		pvswprintf = (int(__cdecl*)(WCHAR*,const WCHAR*,va_list)) GetProcAddress(hModMsvcrt, "vswprintf");
	}

	va_start(ap, fmt);
	ret = (*pvswprintf)(buffer, fmt, ap);
	va_end(ap);

	return ret;
}

static LPCWSTR my_wcsrchr(LPCWSTR str, WCHAR c)
{
	LPCWSTR p = str;

	while(*p)
		++p;

	do {
		if (--p < str)
			return NULL;
	} while(*p != c);

	return p;
}

#define _tcsrchr my_wcsrchr
#else	/* UNICODE */
#define _tcsrchr strrchr
#endif	/* UNICODE */

#endif	/* __WINE__ */


372
/* allocate and initialise a directory entry */
373
static Entry* alloc_entry(void)
374
{
375
	Entry* entry = HeapAlloc(GetProcessHeap(), 0, sizeof(Entry));
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393

#ifdef _SHELL_FOLDERS
	entry->pidl = NULL;
	entry->folder = NULL;
	entry->hicon = 0;
#endif

	return entry;
}

/* free a directory entry */
static void free_entry(Entry* entry)
{
#ifdef _SHELL_FOLDERS
	if (entry->hicon && entry->hicon!=(HICON)-1)
		DestroyIcon(entry->hicon);

	if (entry->folder && entry->folder!=Globals.iDesktop)
394
		IShellFolder_Release(entry->folder);
395 396

	if (entry->pidl)
397
		IMalloc_Free(Globals.iMalloc, entry->pidl);
398 399
#endif

400
	HeapFree(GetProcessHeap(), 0, entry);
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
}

/* recursively free all child entries */
static void free_entries(Entry* dir)
{
	Entry *entry, *next=dir->down;

	if (next) {
		dir->down = 0;

		do {
			entry = next;
			next = entry->next;

			free_entries(entry);
			free_entry(entry);
		} while(next);
	}
}


static void read_directory_win(Entry* dir, LPCTSTR path)
{
	Entry* first_entry = NULL;
	Entry* last = NULL;
	Entry* entry;

	int level = dir->level + 1;
	WIN32_FIND_DATA w32fd;
430 431 432 433 434 435 436 437 438
	HANDLE hFind;
#ifndef _NO_EXTENSIONS
	HANDLE hFile;
#endif

	TCHAR buffer[MAX_PATH], *p;
	for(p=buffer; *path; )
		*p++ = *path++;

439 440 441
	*p++ = '\\';
	p[0] = '*';
	p[1] = '\0';
442

443
	hFind = FindFirstFile(buffer, &w32fd);
444 445 446

	if (hFind != INVALID_HANDLE_VALUE) {
		do {
447 448 449 450 451 452 453 454 455
#ifdef _NO_EXTENSIONS
			/* hide directory entry "." */
			if (w32fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
				LPCTSTR name = w32fd.cFileName;

				if (name[0]=='.' && name[1]=='\0')
					continue;
			}
#endif
456 457 458 459 460 461 462 463 464 465 466
			entry = alloc_entry();

			if (!first_entry)
				first_entry = entry;

			if (last)
				last->next = entry;

			memcpy(&entry->data, &w32fd, sizeof(WIN32_FIND_DATA));
			entry->down = NULL;
			entry->up = dir;
467 468 469 470
			entry->expanded = FALSE;
			entry->scanned = FALSE;
			entry->level = level;

471
#ifndef _NO_EXTENSIONS
472
			entry->etype = ET_WINDOWS;
473 474
			entry->bhfi_valid = FALSE;

475
			lstrcpy(p, entry->data.cFileName);
476 477 478 479 480 481 482 483 484 485 486 487 488

			hFile = CreateFile(buffer, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
								0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0);

			if (hFile != INVALID_HANDLE_VALUE) {
				if (GetFileInformationByHandle(hFile, &entry->bhfi))
					entry->bhfi_valid = TRUE;

				CloseHandle(hFile);
			}
#endif

			last = entry;
489
		} while(FindNextFile(hFind, &w32fd));
490

491 492
		if (last)
			last->next = NULL;
493 494

		FindClose(hFind);
495
	}
496

497 498
	dir->down = first_entry;
	dir->scanned = TRUE;
499 500 501
}


502
static Entry* find_entry_win(Entry* dir, LPCTSTR name)
503 504 505
{
	Entry* entry;

506
	for(entry=dir->down; entry; entry=entry->next) {
507 508 509 510
		LPCTSTR p = name;
		LPCTSTR q = entry->data.cFileName;

		do {
511
			if (!*p || *p == '\\' || *p == '/')
512 513 514 515 516 517 518
				return entry;
		} while(tolower(*p++) == tolower(*q++));

		p = name;
		q = entry->data.cAlternateFileName;

		do {
519
			if (!*p || *p == '\\' || *p == '/')
520 521 522 523 524 525 526 527
				return entry;
		} while(tolower(*p++) == tolower(*q++));
	}

	return 0;
}


528
static Entry* read_tree_win(Root* root, LPCTSTR path, SORT_ORDER sortOrder, HWND hwnd)
529 530 531 532 533 534
{
	TCHAR buffer[MAX_PATH];
	Entry* entry = &root->entry;
	LPCTSTR s = path;
	PTSTR d = buffer;

535 536
	HCURSOR old_cursor = SetCursor(LoadCursor(0, IDC_WAIT));

537
#ifndef _NO_EXTENSIONS
538
	entry->etype = ET_WINDOWS;
539 540 541
#endif

	while(entry) {
542
		while(*s && *s != '\\' && *s != '/')
543 544
			*d++ = *s++;

545
		while(*s == '\\' || *s == '/')
546 547
			s++;

548 549
		*d++ = '\\';
		*d = '\0';
550

551
		read_directory(entry, buffer, sortOrder, hwnd);
552 553 554 555 556 557 558 559 560 561

		if (entry->down)
			entry->expanded = TRUE;

		if (!*s)
			break;

		entry = find_entry_win(entry, s);
	}

562 563
	SetCursor(old_cursor);

564 565 566 567
	return entry;
}


568
#if !defined(_NO_EXTENSIONS) && defined(__WINE__)
569

570
static BOOL time_to_filetime(const time_t* t, FILETIME* ftime)
571 572 573 574 575 576 577 578 579
{
	struct tm* tm = gmtime(t);
	SYSTEMTIME stime;

	if (!tm)
		return FALSE;

	stime.wYear = tm->tm_year+1900;
	stime.wMonth = tm->tm_mon+1;
580
	/*	stime.wDayOfWeek */
581 582 583 584 585 586 587 588
	stime.wDay = tm->tm_mday;
	stime.wHour = tm->tm_hour;
	stime.wMinute = tm->tm_min;
	stime.wSecond = tm->tm_sec;

	return SystemTimeToFileTime(&stime, ftime);
}

589
static void read_directory_unix(Entry* dir, LPCTSTR path)
590
{
591 592 593
	Entry* first_entry = NULL;
	Entry* last = NULL;
	Entry* entry;
594
	DIR* pdir;
595

596
	int level = dir->level + 1;
597 598
#ifdef UNICODE
	char cpath[MAX_PATH];
599

600 601 602 603 604 605
	WideCharToMultiByte(CP_UNIXCP, 0, path, -1, cpath, MAX_PATH, NULL, NULL);
#else
	const char* cpath = path;
#endif

	pdir = opendir(cpath);
606 607

	if (pdir) {
608 609
		struct stat st;
		struct dirent* ent;
610 611
		char buffer[MAX_PATH], *p;
		const char* s;
612

613 614
		for(p=buffer,s=cpath; *s; )
			*p++ = *s++;
615 616 617 618

		if (p==buffer || p[-1]!='/')
			*p++ = '/';

619 620 621 622 623 624 625 626 627 628
		while((ent=readdir(pdir))) {
			entry = alloc_entry();

			if (!first_entry)
				first_entry = entry;

			if (last)
				last->next = entry;

			entry->etype = ET_UNIX;
629 630

			strcpy(p, ent->d_name);
631 632 633 634 635
#ifdef UNICODE
			MultiByteToWideChar(CP_UNIXCP, 0, p, -1, entry->data.cFileName, MAX_PATH);
#else
			lstrcpy(entry->data.cFileName, p);
#endif
636 637

			if (!stat(buffer, &st)) {
638 639
				entry->data.dwFileAttributes = p[0]=='.'? FILE_ATTRIBUTE_HIDDEN: 0;

640 641 642 643 644 645 646
				if (S_ISDIR(st.st_mode))
					entry->data.dwFileAttributes |= FILE_ATTRIBUTE_DIRECTORY;

				entry->data.nFileSizeLow = st.st_size & 0xFFFFFFFF;
				entry->data.nFileSizeHigh = st.st_size >> 32;

				memset(&entry->data.ftCreationTime, 0, sizeof(FILETIME));
647 648
				time_to_filetime(&st.st_atime, &entry->data.ftLastAccessTime);
				time_to_filetime(&st.st_mtime, &entry->data.ftLastWriteTime);
649 650 651 652 653 654 655 656 657 658 659 660 661

				entry->bhfi.nFileIndexLow = ent->d_ino;
				entry->bhfi.nFileIndexHigh = 0;

				entry->bhfi.nNumberOfLinks = st.st_nlink;

				entry->bhfi_valid = TRUE;
			} else {
				entry->data.nFileSizeLow = 0;
				entry->data.nFileSizeHigh = 0;
				entry->bhfi_valid = FALSE;
			}

662 663
			entry->down = NULL;
			entry->up = dir;
664 665 666 667 668 669 670
			entry->expanded = FALSE;
			entry->scanned = FALSE;
			entry->level = level;

			last = entry;
		}

671 672
		if (last)
			last->next = NULL;
673

674 675
		closedir(pdir);
	}
676

677 678
	dir->down = first_entry;
	dir->scanned = TRUE;
679 680
}

681
static Entry* find_entry_unix(Entry* dir, LPCTSTR name)
682 683 684
{
	Entry* entry;

685
	for(entry=dir->down; entry; entry=entry->next) {
686 687 688 689
		LPCTSTR p = name;
		LPCTSTR q = entry->data.cFileName;

		do {
690
			if (!*p || *p == '/')
691 692 693 694 695 696 697
				return entry;
		} while(*p++ == *q++);
	}

	return 0;
}

698
static Entry* read_tree_unix(Root* root, LPCTSTR path, SORT_ORDER sortOrder, HWND hwnd)
699 700 701 702 703 704
{
	TCHAR buffer[MAX_PATH];
	Entry* entry = &root->entry;
	LPCTSTR s = path;
	PTSTR d = buffer;

705 706 707
	HCURSOR old_cursor = SetCursor(LoadCursor(0, IDC_WAIT));

	entry->etype = ET_UNIX;
708 709

	while(entry) {
710
		while(*s && *s != '/')
711 712
			*d++ = *s++;

713
		while(*s == '/')
714 715
			s++;

716 717
		*d++ = '/';
		*d = '\0';
718

719
		read_directory(entry, buffer, sortOrder, hwnd);
720 721 722 723 724 725 726 727 728 729

		if (entry->down)
			entry->expanded = TRUE;

		if (!*s)
			break;

		entry = find_entry_unix(entry, s);
	}

730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
	SetCursor(old_cursor);

	return entry;
}

#endif /* !defined(_NO_EXTENSIONS) && defined(__WINE__) */


#ifdef _SHELL_FOLDERS

#ifdef UNICODE
#define	get_strret get_strretW
#define	path_from_pidl path_from_pidlW
#else
#define	get_strret get_strretA
#define	path_from_pidl path_from_pidlA
#endif


749
static void free_strret(STRRET* str)
750
{
751
	if (str->uType == STRRET_WSTR)
752
		IMalloc_Free(Globals.iMalloc, str->UNION_MEMBER(pOleStr));
753
}
754 755


756
#ifndef UNICODE
757

758
static LPSTR strcpyn(LPSTR dest, LPCSTR source, size_t count)
759
{
760 761
 LPCSTR s;
 LPSTR d = dest;
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784

 for(s=source; count&&(*d++=*s++); )
  count--;

 return dest;
}

static void get_strretA(STRRET* str, const SHITEMID* shiid, LPSTR buffer, int len)
{
 switch(str->uType) {
  case STRRET_WSTR:
	WideCharToMultiByte(CP_ACP, 0, str->UNION_MEMBER(pOleStr), -1, buffer, len, NULL, NULL);
	break;

  case STRRET_OFFSET:
	strcpyn(buffer, (LPCSTR)shiid+str->UNION_MEMBER(uOffset), len);
	break;

  case STRRET_CSTR:
	strcpyn(buffer, str->UNION_MEMBER(cStr), len);
 }
}

785 786 787 788 789
static HRESULT path_from_pidlA(IShellFolder* folder, LPITEMIDLIST pidl, LPSTR buffer, int len)
{
	STRRET str;

	 /* SHGDN_FORPARSING: get full path of id list */
790
	HRESULT hr = IShellFolder_GetDisplayNameOf(folder, pidl, SHGDN_FORPARSING, &str);
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813

	if (SUCCEEDED(hr)) {
		get_strretA(&str, &pidl->mkid, buffer, len);
		free_strret(&str);
	} else
		buffer[0] = '\0';

	return hr;
}

#endif

static LPWSTR wcscpyn(LPWSTR dest, LPCWSTR source, size_t count)
{
 LPCWSTR s;
 LPWSTR d = dest;

 for(s=source; count&&(*d++=*s++); )
  count--;

 return dest;
}

814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
static void get_strretW(STRRET* str, const SHITEMID* shiid, LPWSTR buffer, int len)
{
 switch(str->uType) {
  case STRRET_WSTR:
	wcscpyn(buffer, str->UNION_MEMBER(pOleStr), len);
	break;

  case STRRET_OFFSET:
	MultiByteToWideChar(CP_ACP, 0, (LPCSTR)shiid+str->UNION_MEMBER(uOffset), -1, buffer, len);
	break;

  case STRRET_CSTR:
	MultiByteToWideChar(CP_ACP, 0, str->UNION_MEMBER(cStr), -1, buffer, len);
 }
}


Mike McCormack's avatar
Mike McCormack committed
831
static HRESULT name_from_pidl(IShellFolder* folder, LPITEMIDLIST pidl, LPTSTR buffer, int len, SHGDNF flags)
832 833 834
{
	STRRET str;

835
	HRESULT hr = IShellFolder_GetDisplayNameOf(folder, pidl, flags, &str);
836 837 838 839 840 841 842 843 844 845 846

	if (SUCCEEDED(hr)) {
		get_strret(&str, &pidl->mkid, buffer, len);
		free_strret(&str);
	} else
		buffer[0] = '\0';

	return hr;
}


Mike McCormack's avatar
Mike McCormack committed
847
static HRESULT path_from_pidlW(IShellFolder* folder, LPITEMIDLIST pidl, LPWSTR buffer, int len)
848 849 850 851
{
	STRRET str;

	 /* SHGDN_FORPARSING: get full path of id list */
852
	HRESULT hr = IShellFolder_GetDisplayNameOf(folder, pidl, SHGDN_FORPARSING, &str);
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878

	if (SUCCEEDED(hr)) {
		get_strretW(&str, &pidl->mkid, buffer, len);
		free_strret(&str);
	} else
		buffer[0] = '\0';

	return hr;
}


 /* create an item id list from a file system path */

static LPITEMIDLIST get_path_pidl(LPTSTR path, HWND hwnd)
{
	LPITEMIDLIST pidl;
	HRESULT hr;
	ULONG len;

#ifdef UNICODE
	LPWSTR buffer = path;
#else
	WCHAR buffer[MAX_PATH];
	MultiByteToWideChar(CP_ACP, 0, path, -1, buffer, MAX_PATH);
#endif

879
	hr = IShellFolder_ParseDisplayName(Globals.iDesktop, hwnd, NULL, buffer, &len, &pidl, NULL);
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
	if (FAILED(hr))
		return NULL;

	return pidl;
}


 /* convert an item id list from relative to absolute (=relative to the desktop) format */

static LPITEMIDLIST get_to_absolute_pidl(Entry* entry, HWND hwnd)
{
	if (entry->up && entry->up->etype==ET_SHELL) {
		IShellFolder* folder = entry->up->folder;
		WCHAR buffer[MAX_PATH];

		HRESULT hr = path_from_pidlW(folder, entry->pidl, buffer, MAX_PATH);

		if (SUCCEEDED(hr)) {
			LPITEMIDLIST pidl;
			ULONG len;

901
			hr = IShellFolder_ParseDisplayName(Globals.iDesktop, hwnd, NULL, buffer, &len, &pidl, NULL);
902 903 904 905

			if (SUCCEEDED(hr))
				return pidl;
		}
906 907 908 909
	} else if (entry->etype == ET_WINDOWS) {
		TCHAR path[MAX_PATH];

		get_path(entry, path);
910

911 912 913 914 915
		return get_path_pidl(path, hwnd);
	} else if (entry->pidl)
		return ILClone(entry->pidl);

	return NULL;
916 917 918
}


Mike McCormack's avatar
Mike McCormack committed
919
static HICON extract_icon(IShellFolder* folder, LPCITEMIDLIST pidl)
920 921 922
{
	IExtractIcon* pExtract;

923
	if (SUCCEEDED(IShellFolder_GetUIObjectOf(folder, 0, 1, (LPCITEMIDLIST*)&pidl, &IID_IExtractIcon, 0, (LPVOID*)&pExtract))) {
924 925 926 927 928
		TCHAR path[_MAX_PATH];
		unsigned flags;
		HICON hicon;
		int idx;

929
		if (SUCCEEDED(IExtractIconW_GetIconLocation(pExtract, GIL_FORSHELL, path, _MAX_PATH, &idx, &flags))) {
930 931 932 933 934 935 936 937 938
			if (!(flags & GIL_NOTFILENAME)) {
				if (idx == -1)
					idx = 0;	/* special case for some control panel applications */

				if ((int)ExtractIconEx(path, idx, 0, &hicon, 1) > 0)
					flags &= ~GIL_DONTCACHE;
			} else {
				HICON hIconLarge = 0;

939
				HRESULT hr = IExtractIconW_Extract(pExtract, path, idx, &hIconLarge, &hicon, MAKELONG(0/*GetSystemMetrics(SM_CXICON)*/,GetSystemMetrics(SM_CXSMICON)));
940 941 942 943 944 945 946 947 948 949 950 951 952

				if (SUCCEEDED(hr))
					DestroyIcon(hIconLarge);
			}

			return hicon;
		}
	}

	return 0;
}


953
static Entry* find_entry_shell(Entry* dir, LPCITEMIDLIST pidl)
954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990
{
	Entry* entry;

	for(entry=dir->down; entry; entry=entry->next) {
		if (entry->pidl->mkid.cb == pidl->mkid.cb &&
			!memcmp(entry->pidl, pidl, entry->pidl->mkid.cb))
			return entry;
	}

	return 0;
}

static Entry* read_tree_shell(Root* root, LPITEMIDLIST pidl, SORT_ORDER sortOrder, HWND hwnd)
{
	Entry* entry = &root->entry;
	Entry* next;
	LPITEMIDLIST next_pidl = pidl;
	IShellFolder* folder;
	IShellFolder* child = NULL;
	HRESULT hr;

	HCURSOR old_cursor = SetCursor(LoadCursor(0, IDC_WAIT));

#ifndef _NO_EXTENSIONS
	entry->etype = ET_SHELL;
#endif

	folder = Globals.iDesktop;

	while(entry) {
		entry->pidl = next_pidl;
		entry->folder = folder;

		if (!pidl->mkid.cb)
			break;

		 /* copy first element of item idlist */
991
		next_pidl = IMalloc_Alloc(Globals.iMalloc, pidl->mkid.cb+sizeof(USHORT));
992 993 994
		memcpy(next_pidl, pidl, pidl->mkid.cb);
		((LPITEMIDLIST)((LPBYTE)next_pidl+pidl->mkid.cb))->mkid.cb = 0;

995
		hr = IShellFolder_BindToObject(folder, next_pidl, 0, &IID_IShellFolder, (void**)&child);
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
		if (!SUCCEEDED(hr))
			break;

		read_directory(entry, NULL, sortOrder, hwnd);

		if (entry->down)
			entry->expanded = TRUE;

		next = find_entry_shell(entry, next_pidl);
		if (!next)
			break;

		folder = child;
		entry = next;

		 /* go to next element */
		pidl = (LPITEMIDLIST) ((LPBYTE)pidl+pidl->mkid.cb);
	}

	SetCursor(old_cursor);

1017 1018 1019
	return entry;
}

1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030

static void fill_w32fdata_shell(IShellFolder* folder, LPCITEMIDLIST pidl, SFGAOF attribs, WIN32_FIND_DATA* w32fdata)
{
	if (!(attribs & SFGAO_FILESYSTEM) ||
		  FAILED(SHGetDataFromIDList(folder, pidl, SHGDFIL_FINDDATA, w32fdata, sizeof(WIN32_FIND_DATA)))) {
		WIN32_FILE_ATTRIBUTE_DATA fad;
		IDataObject* pDataObj;

		STGMEDIUM medium = {0, {0}, 0};
		FORMATETC fmt = {Globals.cfStrFName, 0, DVASPECT_CONTENT, -1, TYMED_HGLOBAL};

1031
		HRESULT hr = IShellFolder_GetUIObjectOf(folder, 0, 1, &pidl, &IID_IDataObject, 0, (LPVOID*)&pDataObj);
1032 1033

		if (SUCCEEDED(hr)) {
1034
			hr = IDataObject_GetData(pDataObj, &fmt, &medium);
1035

1036
			IDataObject_Release(pDataObj);
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088

			if (SUCCEEDED(hr)) {
				LPCTSTR path = (LPCTSTR)GlobalLock(medium.UNION_MEMBER(hGlobal));
				UINT sem_org = SetErrorMode(SEM_FAILCRITICALERRORS);

				if (GetFileAttributesEx(path, GetFileExInfoStandard, &fad)) {
					w32fdata->dwFileAttributes = fad.dwFileAttributes;
					w32fdata->ftCreationTime = fad.ftCreationTime;
					w32fdata->ftLastAccessTime = fad.ftLastAccessTime;
					w32fdata->ftLastWriteTime = fad.ftLastWriteTime;

					if (!(fad.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
						w32fdata->nFileSizeLow = fad.nFileSizeLow;
						w32fdata->nFileSizeHigh = fad.nFileSizeHigh;
					}
				}

				SetErrorMode(sem_org);

				GlobalUnlock(medium.UNION_MEMBER(hGlobal));
				GlobalFree(medium.UNION_MEMBER(hGlobal));
			}
		}
	}

	if (attribs & (SFGAO_FOLDER|SFGAO_HASSUBFOLDER))
		w32fdata->dwFileAttributes |= FILE_ATTRIBUTE_DIRECTORY;

	if (attribs & SFGAO_READONLY)
		w32fdata->dwFileAttributes |= FILE_ATTRIBUTE_READONLY;

	if (attribs & SFGAO_COMPRESSED)
		w32fdata->dwFileAttributes |= FILE_ATTRIBUTE_COMPRESSED;
}


static void read_directory_shell(Entry* dir, HWND hwnd)
{
	IShellFolder* folder = dir->folder;
	int level = dir->level + 1;
	HRESULT hr;

	IShellFolder* child;
	IEnumIDList* idlist;

	Entry* first_entry = NULL;
	Entry* last = NULL;
	Entry* entry;

	if (!folder)
		return;

1089
	hr = IShellFolder_EnumObjects(folder, hwnd, SHCONTF_FOLDERS|SHCONTF_NONFOLDERS|SHCONTF_INCLUDEHIDDEN|SHCONTF_SHAREABLE|SHCONTF_STORAGE, &idlist);
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100

	if (SUCCEEDED(hr)) {
		for(;;) {
#define	FETCH_ITEM_COUNT	32
			LPITEMIDLIST pidls[FETCH_ITEM_COUNT];
			SFGAOF attribs;
			ULONG cnt = 0;
			ULONG n;

			memset(pidls, 0, sizeof(pidls));

1101
			hr = IEnumIDList_Next(idlist, FETCH_ITEM_COUNT, pidls, &cnt);
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
			if (!SUCCEEDED(hr))
				break;

			if (hr == S_FALSE)
				break;

			for(n=0; n<cnt; ++n) {
				entry = alloc_entry();

				if (!first_entry)
					first_entry = entry;

				if (last)
					last->next = entry;

				memset(&entry->data, 0, sizeof(WIN32_FIND_DATA));
				entry->bhfi_valid = FALSE;

				attribs = ~SFGAO_FILESYSTEM;	/*SFGAO_HASSUBFOLDER|SFGAO_FOLDER; SFGAO_FILESYSTEM sorgt dafr, da "My Documents" anstatt von "Martin's Documents" angezeigt wird */

1122
				hr = IShellFolder_GetAttributesOf(folder, 1, (LPCITEMIDLIST*)&pidls[n], &attribs);
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136

				if (SUCCEEDED(hr)) {
					if (attribs != (SFGAOF)~SFGAO_FILESYSTEM) {
						fill_w32fdata_shell(folder, pidls[n], attribs, &entry->data);

						entry->bhfi_valid = TRUE;
					} else
						attribs = 0;
				} else
					attribs = 0;

				entry->pidl = pidls[n];

				if (entry->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
1137
					hr = IShellFolder_BindToObject(folder, pidls[n], 0, &IID_IShellFolder, (void**)&child);
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167

					if (SUCCEEDED(hr))
						entry->folder = child;
					else
						entry->folder = NULL;
				}
				else
					entry->folder = NULL;

				if (!entry->data.cFileName[0])
					/*hr = */name_from_pidl(folder, pidls[n], entry->data.cFileName, MAX_PATH, /*SHGDN_INFOLDER*/0x2000/*0x2000=SHGDN_INCLUDE_NONFILESYS*/);

				 /* get display icons for files and virtual objects */
				if (!(entry->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
					!(attribs & SFGAO_FILESYSTEM)) {
					entry->hicon = extract_icon(folder, pidls[n]);

					if (!entry->hicon)
						entry->hicon = (HICON)-1;	/* don't try again later */
				}

				entry->down = NULL;
				entry->up = dir;
				entry->expanded = FALSE;
				entry->scanned = FALSE;
				entry->level = level;

#ifndef _NO_EXTENSIONS
				entry->etype = ET_SHELL;
				entry->bhfi_valid = FALSE;
1168 1169
#endif

1170 1171 1172 1173
				last = entry;
			}
		}

1174
		IEnumIDList_Release(idlist);
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
	}

	if (last)
		last->next = NULL;

	dir->down = first_entry;
	dir->scanned = TRUE;
}

#endif /* _SHELL_FOLDERS */

1186

1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
/* sort order for different directory/file types */
enum TYPE_ORDER {
	TO_DIR = 0,
	TO_DOT = 1,
	TO_DOTDOT = 2,
	TO_OTHER_DIR = 3,
	TO_FILE = 4
};

/* distinguish between ".", ".." and any other directory names */
static int TypeOrderFromDirname(LPCTSTR name)
{
	if (name[0] == '.') {
		if (name[1] == '\0')
			return TO_DOT;	/* "." */

		if (name[1]=='.' && name[2]=='\0')
			return TO_DOTDOT;	/* ".." */
	}

	return TO_OTHER_DIR;	/* anything else */
}

1210
/* directories first... */
1211 1212
static int compareType(const WIN32_FIND_DATA* fd1, const WIN32_FIND_DATA* fd2)
{
1213 1214 1215 1216 1217 1218 1219 1220
	int order1 = fd1->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY? TO_DIR: TO_FILE;
	int order2 = fd2->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY? TO_DIR: TO_FILE;

	/* Handle "." and ".." as special case and move them at the very first beginning. */
	if (order1==TO_DIR && order2==TO_DIR) {
		order1 = TypeOrderFromDirname(fd1->cFileName);
		order2 = TypeOrderFromDirname(fd2->cFileName);
	}
1221

1222
	return order2==order1? 0: order1<order2? -1: 1;
1223 1224 1225 1226 1227
}


static int compareName(const void* arg1, const void* arg2)
{
Eric Pouech's avatar
Eric Pouech committed
1228 1229
	const WIN32_FIND_DATA* fd1 = &(*(const Entry* const*)arg1)->data;
	const WIN32_FIND_DATA* fd2 = &(*(const Entry* const*)arg2)->data;
1230 1231 1232 1233 1234 1235 1236 1237 1238 1239

	int cmp = compareType(fd1, fd2);
	if (cmp)
		return cmp;

	return lstrcmpi(fd1->cFileName, fd2->cFileName);
}

static int compareExt(const void* arg1, const void* arg2)
{
Eric Pouech's avatar
Eric Pouech committed
1240 1241
	const WIN32_FIND_DATA* fd1 = &(*(const Entry* const*)arg1)->data;
	const WIN32_FIND_DATA* fd2 = &(*(const Entry* const*)arg2)->data;
1242 1243 1244 1245 1246 1247 1248 1249 1250
	const TCHAR *name1, *name2, *ext1, *ext2;

	int cmp = compareType(fd1, fd2);
	if (cmp)
		return cmp;

	name1 = fd1->cFileName;
	name2 = fd2->cFileName;

1251 1252
	ext1 = _tcsrchr(name1, '.');
	ext2 = _tcsrchr(name2, '.');
1253 1254 1255 1256

	if (ext1)
		ext1++;
	else
1257
		ext1 = sEmpty;
1258 1259 1260 1261

	if (ext2)
		ext2++;
	else
1262
		ext2 = sEmpty;
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272

	cmp = lstrcmpi(ext1, ext2);
	if (cmp)
		return cmp;

	return lstrcmpi(name1, name2);
}

static int compareSize(const void* arg1, const void* arg2)
{
Eric Pouech's avatar
Eric Pouech committed
1273 1274
	const WIN32_FIND_DATA* fd1 = &(*(const Entry* const*)arg1)->data;
	const WIN32_FIND_DATA* fd2 = &(*(const Entry* const*)arg2)->data;
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293

	int cmp = compareType(fd1, fd2);
	if (cmp)
		return cmp;

	cmp = fd2->nFileSizeHigh - fd1->nFileSizeHigh;

	if (cmp < 0)
		return -1;
	else if (cmp > 0)
		return 1;

	cmp = fd2->nFileSizeLow - fd1->nFileSizeLow;

	return cmp<0? -1: cmp>0? 1: 0;
}

static int compareDate(const void* arg1, const void* arg2)
{
Eric Pouech's avatar
Eric Pouech committed
1294 1295
	const WIN32_FIND_DATA* fd1 = &(*(const Entry* const*)arg1)->data;
	const WIN32_FIND_DATA* fd2 = &(*(const Entry* const*)arg2)->data;
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305

	int cmp = compareType(fd1, fd2);
	if (cmp)
		return cmp;

	return CompareFileTime(&fd2->ftLastWriteTime, &fd1->ftLastWriteTime);
}


static int (*sortFunctions[])(const void* arg1, const void* arg2) = {
1306
	compareName,	/* SORT_NAME */
1307 1308 1309
	compareExt,		/* SORT_EXT */
	compareSize,	/* SORT_SIZE */
	compareDate		/* SORT_DATE */
1310 1311 1312
};


1313
static void SortDirectory(Entry* dir, SORT_ORDER sortOrder)
1314
{
1315
	Entry* entry = dir->down;
1316 1317 1318 1319
	Entry** array, **p;
	int len;

	len = 0;
1320
	for(entry=dir->down; entry; entry=entry->next)
1321 1322 1323
		len++;

	if (len) {
Alexandre Julliard's avatar
Alexandre Julliard committed
1324
		array = HeapAlloc(GetProcessHeap(), 0, len*sizeof(Entry*));
1325 1326

		p = array;
1327
		for(entry=dir->down; entry; entry=entry->next)
1328 1329
			*p++ = entry;

1330
		/* call qsort with the appropriate compare function */
1331 1332
		qsort(array, len, sizeof(array[0]), sortFunctions[sortOrder]);

1333
		dir->down = array[0];
1334 1335 1336 1337 1338

		for(p=array; --len; p++)
			p[0]->next = p[1];

		(*p)->next = 0;
1339 1340

                HeapFree(GetProcessHeap(), 0, array);
1341 1342 1343 1344
	}
}


1345
static void read_directory(Entry* dir, LPCTSTR path, SORT_ORDER sortOrder, HWND hwnd)
1346 1347 1348 1349 1350 1351
{
	TCHAR buffer[MAX_PATH];
	Entry* entry;
	LPCTSTR s;
	PTSTR d;

1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363
#ifdef _SHELL_FOLDERS
	if (dir->etype == ET_SHELL)
	{
		read_directory_shell(dir, hwnd);

		if (Globals.prescan_node) {
			s = path;
			d = buffer;

			while(*s)
				*d++ = *s++;

1364
			*d++ = '\\';
1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376

			for(entry=dir->down; entry; entry=entry->next)
				if (entry->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
					read_directory_shell(entry, hwnd);
					SortDirectory(entry, sortOrder);
				}
		}
	}
	else
#endif
#if !defined(_NO_EXTENSIONS) && defined(__WINE__)
	if (dir->etype == ET_UNIX)
1377
	{
1378
		read_directory_unix(dir, path);
1379 1380 1381 1382 1383 1384 1385 1386

		if (Globals.prescan_node) {
			s = path;
			d = buffer;

			while(*s)
				*d++ = *s++;

1387
			*d++ = '/';
1388

1389
			for(entry=dir->down; entry; entry=entry->next)
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
				if (entry->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
					lstrcpy(d, entry->data.cFileName);
					read_directory_unix(entry, buffer);
					SortDirectory(entry, sortOrder);
				}
		}
	}
	else
#endif
	{
1400
		read_directory_win(dir, path);
1401 1402 1403 1404 1405 1406 1407 1408

		if (Globals.prescan_node) {
			s = path;
			d = buffer;

			while(*s)
				*d++ = *s++;

1409
			*d++ = '\\';
1410

1411
			for(entry=dir->down; entry; entry=entry->next)
1412 1413 1414 1415 1416 1417 1418 1419
				if (entry->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
					lstrcpy(d, entry->data.cFileName);
					read_directory_win(entry, buffer);
					SortDirectory(entry, sortOrder);
				}
		}
	}

1420
	SortDirectory(dir, sortOrder);
1421 1422 1423
}


1424
static Entry* read_tree(Root* root, LPCTSTR path, LPITEMIDLIST pidl, LPTSTR drv, SORT_ORDER sortOrder, HWND hwnd)
1425
{
1426
#if !defined(_NO_EXTENSIONS) && defined(__WINE__)
1427
	static const TCHAR sSlash[] = {'/', '\0'};
1428
#endif
1429
	static const TCHAR sBackslash[] = {'\\', '\0'};
1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469

#ifdef _SHELL_FOLDERS
	if (pidl)
	{
		 /* read shell namespace tree */
		root->drive_type = DRIVE_UNKNOWN;
		drv[0] = '\\';
		drv[1] = '\0';
		load_string(root->volname, IDS_DESKTOP);
		root->fs_flags = 0;
		load_string(root->fs, IDS_SHELL);

		return read_tree_shell(root, pidl, sortOrder, hwnd);
	}
	else
#endif
#if !defined(_NO_EXTENSIONS) && defined(__WINE__)
	if (*path == '/')
	{
		 /* read unix file system tree */
		root->drive_type = GetDriveType(path);

		lstrcat(drv, sSlash);
		load_string(root->volname, IDS_ROOT_FS);
		root->fs_flags = 0;
		load_string(root->fs, IDS_UNIXFS);

		lstrcpy(root->path, sSlash);

		return read_tree_unix(root, path, sortOrder, hwnd);
	}
#endif

	 /* read WIN32 file system tree */
	root->drive_type = GetDriveType(path);

	lstrcat(drv, sBackslash);
	GetVolumeInformation(drv, root->volname, _MAX_FNAME, 0, 0, &root->fs_flags, root->fs, _MAX_DIR);

	lstrcpy(root->path, drv);
1470

1471 1472 1473 1474
	return read_tree_win(root, path, sortOrder, hwnd);
}


1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485
/* flags to filter different file types */
enum TYPE_FILTER {
	TF_DIRECTORIES	= 0x01,
	TF_PROGRAMS		= 0x02,
	TF_DOCUMENTS	= 0x04,
	TF_OTHERS		= 0x08,
	TF_HIDDEN		= 0x10,
	TF_ALL			= 0x1F
};


1486 1487
static ChildWnd* alloc_child_window(LPCTSTR path, LPITEMIDLIST pidl, HWND hwnd)
{
1488
	TCHAR drv[_MAX_DRIVE+1], dir[_MAX_DIR], name[_MAX_FNAME], ext[_MAX_EXT];
1489
	TCHAR dir_path[MAX_PATH];
1490
	TCHAR b1[BUFFER_LEN];
1491
	static const TCHAR sAsterics[] = {'*', '\0'};
1492

1493
	ChildWnd* child = HeapAlloc(GetProcessHeap(), 0, sizeof(ChildWnd));
1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517
	Root* root = &child->root;
	Entry* entry;

	memset(child, 0, sizeof(ChildWnd));

	child->left.treePane = TRUE;
	child->left.visible_cols = 0;

	child->right.treePane = FALSE;
#ifndef _NO_EXTENSIONS
	child->right.visible_cols = COL_SIZE|COL_DATE|COL_TIME|COL_ATTRIBUTES|COL_INDEX|COL_LINKS;
#else
	child->right.visible_cols = COL_SIZE|COL_DATE|COL_TIME|COL_ATTRIBUTES;
#endif

	child->pos.length = sizeof(WINDOWPLACEMENT);
	child->pos.flags = 0;
	child->pos.showCmd = SW_SHOWNORMAL;
	child->pos.rcNormalPosition.left = CW_USEDEFAULT;
	child->pos.rcNormalPosition.top = CW_USEDEFAULT;
	child->pos.rcNormalPosition.right = CW_USEDEFAULT;
	child->pos.rcNormalPosition.bottom = CW_USEDEFAULT;

	child->focus_pane = 0;
1518
	child->split_pos = DEFAULT_SPLIT_POS;
1519 1520 1521
	child->sortOrder = SORT_NAME;
	child->header_wdths_ok = FALSE;

1522 1523 1524 1525 1526 1527
	if (path)
	{
		lstrcpy(child->path, path);

		_tsplitpath(path, drv, dir, name, ext);
	}
1528

1529
	lstrcpy(child->filter_pattern, sAsterics);
1530 1531
	child->filter_flags = TF_ALL;

1532 1533
	root->entry.level = 0;

1534 1535 1536
	lstrcpy(dir_path, drv);
	lstrcat(dir_path, dir);
	entry = read_tree(root, dir_path, pidl, drv, child->sortOrder, hwnd);
1537

1538 1539
#ifdef _SHELL_FOLDERS
	if (root->entry.etype == ET_SHELL)
1540
		load_string(root->entry.data.cFileName, IDS_DESKTOP);
1541 1542
	else
#endif
1543
		wsprintf(root->entry.data.cFileName, RS(b1,IDS_TITLEFMT), drv, root->fs);
1544 1545 1546 1547

	root->entry.data.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;

	child->left.root = &root->entry;
1548
	child->right.root = NULL;
1549

1550
	set_curdir(child, entry, 0, hwnd);
1551 1552 1553 1554 1555

	return child;
}


1556
/* free all memory associated with a child window */
1557 1558 1559
static void free_child_window(ChildWnd* child)
{
	free_entries(&child->root.entry);
1560
	HeapFree(GetProcessHeap(), 0, child);
1561 1562 1563
}


1564
/* get full path of specified directory entry */
1565 1566 1567 1568 1569 1570
static void get_path(Entry* dir, PTSTR path)
{
	Entry* entry;
	int len = 0;
	int level = 0;

1571 1572 1573 1574 1575 1576
#ifdef _SHELL_FOLDERS
	if (dir->etype == ET_SHELL)
	{
		SFGAOF attribs;
		HRESULT hr = S_OK;

1577
		path[0] = '\0';
1578 1579 1580 1581

		attribs = 0;

		if (dir->folder)
1582
			hr = IShellFolder_GetAttributesOf(dir->folder, 1, (LPCITEMIDLIST*)&dir->pidl, &attribs);
1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600

		if (SUCCEEDED(hr) && (attribs&SFGAO_FILESYSTEM)) {
			IShellFolder* parent = dir->up? dir->up->folder: Globals.iDesktop;

			hr = path_from_pidl(parent, dir->pidl, path, MAX_PATH);
		}
	}
	else
#endif
	{
		for(entry=dir; entry; level++) {
			LPCTSTR name;
			int l;

			{
				LPCTSTR s;
				name = entry->data.cFileName;
				s = name;
1601

1602
				for(l=0; *s && *s != '/' && *s != '\\'; s++)
1603 1604
					l++;
			}
1605

1606 1607 1608 1609 1610
			if (entry->up) {
				if (l > 0) {
					memmove(path+l+1, path, len*sizeof(TCHAR));
					memcpy(path+1, name, l*sizeof(TCHAR));
					len += l+1;
1611 1612

#ifndef _NO_EXTENSIONS
1613
					if (entry->etype == ET_UNIX)
1614
						path[0] = '/';
1615
					else
1616
#endif
1617
					path[0] = '\\';
1618
				}
1619

1620 1621 1622 1623 1624 1625 1626
				entry = entry->up;
			} else {
				memmove(path+l, path, len*sizeof(TCHAR));
				memcpy(path, name, l*sizeof(TCHAR));
				len += l;
				break;
			}
1627 1628
		}

1629
		if (!level) {
1630
#ifndef _NO_EXTENSIONS
1631
			if (entry->etype == ET_UNIX)
1632
				path[len++] = '/';
1633
			else
1634
#endif
1635
				path[len++] = '\\';
1636
		}
1637

1638
		path[len] = '\0';
1639
	}
1640 1641
}

1642 1643 1644 1645 1646 1647 1648
static windowOptions load_registry_settings(void)
{
	DWORD size;
	DWORD type;
	HKEY hKey;
	windowOptions opts;

1649 1650
        RegOpenKeyExW( HKEY_CURRENT_USER, registry_key,
                       0, KEY_QUERY_VALUE, &hKey );
1651 1652 1653

	size = sizeof(DWORD);

1654 1655
        if( RegQueryValueExW( hKey, reg_start_x, NULL, &type,
                              (LPBYTE) &opts.start_x, &size ) != ERROR_SUCCESS )
1656 1657
		opts.start_x = CW_USEDEFAULT;

1658 1659
        if( RegQueryValueExW( hKey, reg_start_y, NULL, &type,
                              (LPBYTE) &opts.start_y, &size ) != ERROR_SUCCESS )
1660 1661
		opts.start_y = CW_USEDEFAULT;

1662 1663
        if( RegQueryValueExW( hKey, reg_width, NULL, &type,
                              (LPBYTE) &opts.width, &size ) != ERROR_SUCCESS )
1664 1665
		opts.width = CW_USEDEFAULT;

1666 1667
        if( RegQueryValueExW( hKey, reg_height, NULL, &type,
                              (LPBYTE) &opts.height, &size ) != ERROR_SUCCESS )
1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685
		opts.height = CW_USEDEFAULT;

	RegCloseKey( hKey );

	return opts;
}

static void save_registry_settings(void)
{
	WINDOWINFO wi;
	HKEY hKey;
	INT width, height;

	wi.cbSize = sizeof( WINDOWINFO );
	GetWindowInfo(Globals.hMainWnd, &wi);
	width = wi.rcWindow.right - wi.rcWindow.left;
	height = wi.rcWindow.bottom - wi.rcWindow.top;

1686 1687
	if ( RegOpenKeyExW( HKEY_CURRENT_USER, registry_key,
                            0, KEY_SET_VALUE, &hKey ) != ERROR_SUCCESS )
1688 1689
	{
		/* Unable to save registry settings - try to create key */
1690 1691 1692
                if ( RegCreateKeyExW( HKEY_CURRENT_USER, registry_key,
                                      0, NULL, REG_OPTION_NON_VOLATILE,
                                      KEY_SET_VALUE, NULL, &hKey, NULL ) != ERROR_SUCCESS )
1693 1694 1695 1696 1697 1698
		{
			/* FIXME: Cannot create key */
			return;
		}
	}
	/* Save all of the settings */
1699 1700 1701 1702 1703 1704 1705 1706
        RegSetValueExW( hKey, reg_start_x, 0, REG_DWORD,
                        (LPBYTE) &wi.rcWindow.left, sizeof(DWORD) );
        RegSetValueExW( hKey, reg_start_y, 0, REG_DWORD,
                        (LPBYTE) &wi.rcWindow.top, sizeof(DWORD) );
        RegSetValueExW( hKey, reg_width, 0, REG_DWORD,
                        (LPBYTE) &width, sizeof(DWORD) );
        RegSetValueExW( hKey, reg_height, 0, REG_DWORD,
                        (LPBYTE) &height, sizeof(DWORD) );
1707 1708 1709 1710

	/* TODO: Save more settings here (List vs. Detailed View, etc.) */
	RegCloseKey( hKey );
}
1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746

static void resize_frame_rect(HWND hwnd, PRECT prect)
{
	int new_top;
	RECT rt;

	if (IsWindowVisible(Globals.htoolbar)) {
		SendMessage(Globals.htoolbar, WM_SIZE, 0, 0);
		GetClientRect(Globals.htoolbar, &rt);
		prect->top = rt.bottom+3;
		prect->bottom -= rt.bottom+3;
	}

	if (IsWindowVisible(Globals.hdrivebar)) {
		SendMessage(Globals.hdrivebar, WM_SIZE, 0, 0);
		GetClientRect(Globals.hdrivebar, &rt);
		new_top = --prect->top + rt.bottom+3;
		MoveWindow(Globals.hdrivebar, 0, prect->top, rt.right, new_top, TRUE);
		prect->top = new_top;
		prect->bottom -= rt.bottom+2;
	}

	if (IsWindowVisible(Globals.hstatusbar)) {
		int parts[] = {300, 500};

		SendMessage(Globals.hstatusbar, WM_SIZE, 0, 0);
		SendMessage(Globals.hstatusbar, SB_SETPARTS, 2, (LPARAM)&parts);
		GetClientRect(Globals.hstatusbar, &rt);
		prect->bottom -= rt.bottom;
	}

	MoveWindow(Globals.hmdiclient, prect->left-1,prect->top-1,prect->right+2,prect->bottom+1, TRUE);
}

static void resize_frame(HWND hwnd, int cx, int cy)
{
1747 1748 1749 1750 1751 1752
	RECT rect;

	rect.left   = 0;
	rect.top    = 0;
	rect.right  = cx;
	rect.bottom = cy;
1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769

	resize_frame_rect(hwnd, &rect);
}

static void resize_frame_client(HWND hwnd)
{
	RECT rect;

	GetClientRect(hwnd, &rect);

	resize_frame_rect(hwnd, &rect);
}


static HHOOK hcbthook;
static ChildWnd* newchild = NULL;

Mike McCormack's avatar
Mike McCormack committed
1770
static LRESULT CALLBACK CBTProc(int code, WPARAM wparam, LPARAM lparam)
1771 1772 1773 1774 1775 1776
{
	if (code==HCBT_CREATEWND && newchild) {
		ChildWnd* child = newchild;
		newchild = NULL;

		child->hwnd = (HWND) wparam;
1777
		SetWindowLongPtr(child->hwnd, GWLP_USERDATA, (LPARAM)child);
1778 1779 1780 1781 1782 1783 1784
	}

	return CallNextHookEx(hcbthook, code, wparam, lparam);
}

static HWND create_child_window(ChildWnd* child)
{
1785
	MDICREATESTRUCT mcs;
1786 1787
	int idx;

1788
	mcs.szClass = sWINEFILETREE;
1789 1790 1791 1792 1793 1794
	mcs.szTitle = (LPTSTR)child->path;
	mcs.hOwner  = Globals.hInstance;
	mcs.x       = child->pos.rcNormalPosition.left;
	mcs.y       = child->pos.rcNormalPosition.top;
	mcs.cx      = child->pos.rcNormalPosition.right-child->pos.rcNormalPosition.left;
	mcs.cy      = child->pos.rcNormalPosition.bottom-child->pos.rcNormalPosition.top;
1795
	mcs.style   = 0;
1796 1797
	mcs.lParam  = 0;

1798 1799 1800 1801
	hcbthook = SetWindowsHookEx(WH_CBT, CBTProc, 0, GetCurrentThreadId());

	newchild = child;
	child->hwnd = (HWND) SendMessage(Globals.hmdiclient, WM_MDICREATE, 0, (LPARAM)&mcs);
1802 1803
	if (!child->hwnd) {
		UnhookWindowsHookEx(hcbthook);
1804
		return 0;
1805
	}
1806 1807 1808

	UnhookWindowsHookEx(hcbthook);

1809 1810
	SendMessage(child->left.hwnd, LB_SETITEMHEIGHT, 1, max(Globals.spaceSize.cy,IMAGE_HEIGHT+3));
	SendMessage(child->right.hwnd, LB_SETITEMHEIGHT, 1, max(Globals.spaceSize.cy,IMAGE_HEIGHT+3));
1811

1812 1813
	idx = SendMessage(child->left.hwnd, LB_FINDSTRING, 0, (LPARAM)child->left.cur);
	SendMessage(child->left.hwnd, LB_SETCURSEL, idx, 0);
1814 1815 1816 1817 1818 1819 1820 1821 1822 1823

	return child->hwnd;
}


struct ExecuteDialog {
	TCHAR	cmd[MAX_PATH];
	int		cmdshow;
};

1824
static INT_PTR CALLBACK ExecuteDialogDlgProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam)
1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837
{
	static struct ExecuteDialog* dlg;

	switch(nmsg) {
		case WM_INITDIALOG:
			dlg = (struct ExecuteDialog*) lparam;
			return 1;

		case WM_COMMAND: {
			int id = (int)wparam;

			if (id == IDOK) {
				GetWindowText(GetDlgItem(hwnd, 201), dlg->cmd, MAX_PATH);
1838
				dlg->cmdshow = get_check(hwnd,214) ? SW_SHOWMINIMIZED : SW_SHOWNORMAL;
1839 1840 1841 1842 1843 1844 1845 1846 1847 1848
				EndDialog(hwnd, id);
			} else if (id == IDCANCEL)
				EndDialog(hwnd, id);

			return 1;}
	}

	return 0;
}

1849

1850
static INT_PTR CALLBACK DestinationDlgProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam)
Piotr Caban's avatar
Piotr Caban committed
1851
{
1852
	TCHAR b1[BUFFER_LEN], b2[BUFFER_LEN];
1853

Piotr Caban's avatar
Piotr Caban committed
1854 1855
	switch(nmsg) {
		case WM_INITDIALOG:
1856
			SetWindowLongPtr(hwnd, GWLP_USERDATA, lparam);
1857
			SetWindowText(GetDlgItem(hwnd, 201), (LPCTSTR)lparam);
Piotr Caban's avatar
Piotr Caban committed
1858 1859
			return 1;

1860
		case WM_COMMAND: {
Piotr Caban's avatar
Piotr Caban committed
1861
			int id = (int)wparam;
1862 1863 1864

			switch(id) {
			  case IDOK: {
1865
				LPTSTR dest = (LPTSTR) GetWindowLongPtr(hwnd, GWLP_USERDATA);
Piotr Caban's avatar
Piotr Caban committed
1866
				GetWindowText(GetDlgItem(hwnd, 201), dest, MAX_PATH);
1867 1868 1869 1870 1871 1872 1873 1874
				EndDialog(hwnd, id);
				break;}

			  case IDCANCEL:
				EndDialog(hwnd, id);
				break;

			  case 254:
1875
				MessageBox(hwnd, RS(b1,IDS_NO_IMPL), RS(b2,IDS_WINEFILE), MB_OK);
1876
				break;
Piotr Caban's avatar
Piotr Caban committed
1877
			}
1878

Piotr Caban's avatar
Piotr Caban committed
1879
			return 1;
1880
		}
Piotr Caban's avatar
Piotr Caban committed
1881 1882 1883 1884 1885
	}

	return 0;
}

1886

1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899
struct FilterDialog {
	TCHAR	pattern[MAX_PATH];
	int		flags;
};

static INT_PTR CALLBACK FilterDialogDlgProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam)
{
	static struct FilterDialog* dlg;

	switch(nmsg) {
		case WM_INITDIALOG:
			dlg = (struct FilterDialog*) lparam;
			SetWindowText(GetDlgItem(hwnd, IDC_VIEW_PATTERN), dlg->pattern);
1900 1901 1902 1903 1904
			set_check(hwnd, IDC_VIEW_TYPE_DIRECTORIES, dlg->flags&TF_DIRECTORIES);
			set_check(hwnd, IDC_VIEW_TYPE_PROGRAMS, dlg->flags&TF_PROGRAMS);
			set_check(hwnd, IDC_VIEW_TYPE_DOCUMENTS, dlg->flags&TF_DOCUMENTS);
			set_check(hwnd, IDC_VIEW_TYPE_OTHERS, dlg->flags&TF_OTHERS);
			set_check(hwnd, IDC_VIEW_TYPE_HIDDEN, dlg->flags&TF_HIDDEN);
1905 1906 1907 1908 1909 1910 1911 1912 1913 1914
			return 1;

		case WM_COMMAND: {
			int id = (int)wparam;

			if (id == IDOK) {
				int flags = 0;

				GetWindowText(GetDlgItem(hwnd, IDC_VIEW_PATTERN), dlg->pattern, MAX_PATH);

1915 1916 1917 1918 1919
				flags |= get_check(hwnd, IDC_VIEW_TYPE_DIRECTORIES) ? TF_DIRECTORIES : 0;
				flags |= get_check(hwnd, IDC_VIEW_TYPE_PROGRAMS) ? TF_PROGRAMS : 0;
				flags |= get_check(hwnd, IDC_VIEW_TYPE_DOCUMENTS) ? TF_DOCUMENTS : 0;
				flags |= get_check(hwnd, IDC_VIEW_TYPE_OTHERS) ? TF_OTHERS : 0;
				flags |= get_check(hwnd, IDC_VIEW_TYPE_HIDDEN) ? TF_HIDDEN : 0;
1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933

				dlg->flags = flags;

				EndDialog(hwnd, id);
			} else if (id == IDCANCEL)
				EndDialog(hwnd, id);

			return 1;}
	}

	return 0;
}


1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963
struct PropertiesDialog {
	TCHAR	path[MAX_PATH];
	Entry	entry;
	void*	pVersionData;
};

/* Structure used to store enumerated languages and code pages. */
struct LANGANDCODEPAGE {
	WORD wLanguage;
	WORD wCodePage;
} *lpTranslate;

static LPCSTR InfoStrings[] = {
	"Comments",
	"CompanyName",
	"FileDescription",
	"FileVersion",
	"InternalName",
	"LegalCopyright",
	"LegalTrademarks",
	"OriginalFilename",
	"PrivateBuild",
	"ProductName",
	"ProductVersion",
	"SpecialBuild",
	NULL
};

static void PropDlg_DisplayValue(HWND hlbox, HWND hedit)
{
1964
	int idx = SendMessage(hlbox, LB_GETCURSEL, 0, 0);
1965 1966

	if (idx != LB_ERR) {
1967
		LPCTSTR pValue = (LPCTSTR) SendMessage(hlbox, LB_GETITEMDATA, idx, 0);
1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982

		if (pValue)
			SetWindowText(hedit, pValue);
	}
}

static void CheckForFileInfo(struct PropertiesDialog* dlg, HWND hwnd, LPCTSTR strFilename)
{
	static TCHAR sBackSlash[] = {'\\','\0'};
	static TCHAR sTranslation[] = {'\\','V','a','r','F','i','l','e','I','n','f','o','\\','T','r','a','n','s','l','a','t','i','o','n','\0'};
	static TCHAR sStringFileInfo[] = {'\\','S','t','r','i','n','g','F','i','l','e','I','n','f','o','\\',
										'%','0','4','x','%','0','4','x','\\','%','s','\0'};
	DWORD dwVersionDataLen = GetFileVersionInfoSize(strFilename, NULL);

	if (dwVersionDataLen) {
1983
		dlg->pVersionData = HeapAlloc(GetProcessHeap(), 0, dwVersionDataLen);
1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030

		if (GetFileVersionInfo(strFilename, 0, dwVersionDataLen, dlg->pVersionData)) {
			LPVOID pVal;
			UINT nValLen;

			if (VerQueryValue(dlg->pVersionData, sBackSlash, &pVal, &nValLen)) {
				if (nValLen == sizeof(VS_FIXEDFILEINFO)) {
					VS_FIXEDFILEINFO* pFixedFileInfo = (VS_FIXEDFILEINFO*)pVal;
					char buffer[BUFFER_LEN];

					sprintf(buffer, "%d.%d.%d.%d",
						HIWORD(pFixedFileInfo->dwFileVersionMS), LOWORD(pFixedFileInfo->dwFileVersionMS),
						HIWORD(pFixedFileInfo->dwFileVersionLS), LOWORD(pFixedFileInfo->dwFileVersionLS));

					SetDlgItemTextA(hwnd, IDC_STATIC_PROP_VERSION, buffer);
				}
			}

			/* Read the list of languages and code pages. */
			if (VerQueryValue(dlg->pVersionData, sTranslation, &pVal, &nValLen)) {
				struct LANGANDCODEPAGE* pTranslate = (struct LANGANDCODEPAGE*)pVal;
				struct LANGANDCODEPAGE* pEnd = (struct LANGANDCODEPAGE*)((LPBYTE)pVal+nValLen);

				HWND hlbox = GetDlgItem(hwnd, IDC_LIST_PROP_VERSION_TYPES);

				/* Read the file description for each language and code page. */
				for(; pTranslate<pEnd; ++pTranslate) {
					LPCSTR* p;

					for(p=InfoStrings; *p; ++p) {
						TCHAR subblock[200];
#ifdef UNICODE
						TCHAR infoStr[100];
#endif
						LPCTSTR pTxt;
						UINT nValLen;

						LPCSTR pInfoString = *p;
#ifdef UNICODE
						MultiByteToWideChar(CP_ACP, 0, pInfoString, -1, infoStr, 100);
#else
#define	infoStr pInfoString
#endif
						wsprintf(subblock, sStringFileInfo, pTranslate->wLanguage, pTranslate->wCodePage, infoStr);

						/* Retrieve file description for language and code page */
						if (VerQueryValue(dlg->pVersionData, subblock, (PVOID)&pTxt, &nValLen)) {
2031 2032
							int idx = SendMessage(hlbox, LB_ADDSTRING, 0L, (LPARAM)infoStr);
							SendMessage(hlbox, LB_SETITEMDATA, idx, (LPARAM) pTxt);
2033 2034 2035 2036
						}
					}
				}

2037
				SendMessage(hlbox, LB_SETCURSEL, 0, 0);
2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050

				PropDlg_DisplayValue(hlbox, GetDlgItem(hwnd,IDC_LIST_PROP_VERSION_VALUES));
			}
		}
	}
}

static INT_PTR CALLBACK PropertiesDialogDlgProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam)
{
	static struct PropertiesDialog* dlg;

	switch(nmsg) {
		case WM_INITDIALOG: {
2051
			static const TCHAR sByteFmt[] = {'%','s',' ','B','y','t','e','s','\0'};
2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066
			TCHAR b1[BUFFER_LEN], b2[BUFFER_LEN];
			LPWIN32_FIND_DATA pWFD;
			ULONGLONG size;

			dlg = (struct PropertiesDialog*) lparam;
			pWFD = (LPWIN32_FIND_DATA) &dlg->entry.data;

			GetWindowText(hwnd, b1, MAX_PATH);
			wsprintf(b2, b1, pWFD->cFileName);
			SetWindowText(hwnd, b2);

			format_date(&pWFD->ftLastWriteTime, b1, COL_DATE|COL_TIME);
			SetWindowText(GetDlgItem(hwnd, IDC_STATIC_PROP_LASTCHANGE), b1);

			size = ((ULONGLONG)pWFD->nFileSizeHigh << 32) | pWFD->nFileSizeLow;
2067
			_stprintf(b1, sLongNumFmt, size);
2068
			wsprintf(b2, sByteFmt, b1);
2069 2070 2071 2072 2073
			SetWindowText(GetDlgItem(hwnd, IDC_STATIC_PROP_SIZE), b2);

			SetWindowText(GetDlgItem(hwnd, IDC_STATIC_PROP_FILENAME), pWFD->cFileName);
			SetWindowText(GetDlgItem(hwnd, IDC_STATIC_PROP_PATH), dlg->path);

2074 2075 2076 2077 2078
			set_check(hwnd, IDC_CHECK_READONLY, pWFD->dwFileAttributes&FILE_ATTRIBUTE_READONLY);
			set_check(hwnd, IDC_CHECK_ARCHIVE, pWFD->dwFileAttributes&FILE_ATTRIBUTE_ARCHIVE);
			set_check(hwnd, IDC_CHECK_COMPRESSED, pWFD->dwFileAttributes&FILE_ATTRIBUTE_COMPRESSED);
			set_check(hwnd, IDC_CHECK_HIDDEN, pWFD->dwFileAttributes&FILE_ATTRIBUTE_HIDDEN);
			set_check(hwnd, IDC_CHECK_SYSTEM, pWFD->dwFileAttributes&FILE_ATTRIBUTE_SYSTEM);
2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100

			CheckForFileInfo(dlg, hwnd, dlg->path);
			return 1;}

		case WM_COMMAND: {
			int id = (int)wparam;

			switch(HIWORD(wparam)) {
			  case LBN_SELCHANGE: {
				HWND hlbox = GetDlgItem(hwnd, IDC_LIST_PROP_VERSION_TYPES);
				PropDlg_DisplayValue(hlbox, GetDlgItem(hwnd,IDC_LIST_PROP_VERSION_VALUES));
				break;
			  }

			  case BN_CLICKED:
				if (id==IDOK || id==IDCANCEL)
					EndDialog(hwnd, id);
			}

			return 1;}

		case WM_NCDESTROY:
2101
			HeapFree(GetProcessHeap(), 0, dlg->pVersionData);
2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120
			dlg->pVersionData = NULL;
			break;
	}

	return 0;
}

static void show_properties_dlg(Entry* entry, HWND hwnd)
{
	struct PropertiesDialog dlg;

	memset(&dlg, 0, sizeof(struct PropertiesDialog));
	get_path(entry, dlg.path);
	memcpy(&dlg.entry, entry, sizeof(Entry));

	DialogBoxParam(Globals.hInstance, MAKEINTRESOURCE(IDD_DIALOG_PROPERTIES), hwnd, PropertiesDialogDlgProc, (LPARAM)&dlg);
}


2121 2122 2123 2124 2125 2126 2127
#ifndef _NO_EXTENSIONS

static struct FullScreenParameters {
	BOOL	mode;
	RECT	orgPos;
	BOOL	wasZoomed;
} g_fullscreen = {
2128 2129 2130
    FALSE,	/* mode */
	{0, 0, 0, 0},
	FALSE
2131 2132
};

Mike McCormack's avatar
Mike McCormack committed
2133
static void frame_get_clientspace(HWND hwnd, PRECT prect)
2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233
{
	RECT rt;

	if (!IsIconic(hwnd))
		GetClientRect(hwnd, prect);
	else {
		WINDOWPLACEMENT wp;

		GetWindowPlacement(hwnd, &wp);

		prect->left = prect->top = 0;
		prect->right = wp.rcNormalPosition.right-wp.rcNormalPosition.left-
						2*(GetSystemMetrics(SM_CXSIZEFRAME)+GetSystemMetrics(SM_CXEDGE));
		prect->bottom = wp.rcNormalPosition.bottom-wp.rcNormalPosition.top-
						2*(GetSystemMetrics(SM_CYSIZEFRAME)+GetSystemMetrics(SM_CYEDGE))-
						GetSystemMetrics(SM_CYCAPTION)-GetSystemMetrics(SM_CYMENUSIZE);
	}

	if (IsWindowVisible(Globals.htoolbar)) {
		GetClientRect(Globals.htoolbar, &rt);
		prect->top += rt.bottom+2;
	}

	if (IsWindowVisible(Globals.hdrivebar)) {
		GetClientRect(Globals.hdrivebar, &rt);
		prect->top += rt.bottom+2;
	}

	if (IsWindowVisible(Globals.hstatusbar)) {
		GetClientRect(Globals.hstatusbar, &rt);
		prect->bottom -= rt.bottom;
	}
}

static BOOL toggle_fullscreen(HWND hwnd)
{
	RECT rt;

	if ((g_fullscreen.mode=!g_fullscreen.mode)) {
		GetWindowRect(hwnd, &g_fullscreen.orgPos);
		g_fullscreen.wasZoomed = IsZoomed(hwnd);

		Frame_CalcFrameClient(hwnd, &rt);
		ClientToScreen(hwnd, (LPPOINT)&rt.left);
		ClientToScreen(hwnd, (LPPOINT)&rt.right);

		rt.left = g_fullscreen.orgPos.left-rt.left;
		rt.top = g_fullscreen.orgPos.top-rt.top;
		rt.right = GetSystemMetrics(SM_CXSCREEN)+g_fullscreen.orgPos.right-rt.right;
		rt.bottom = GetSystemMetrics(SM_CYSCREEN)+g_fullscreen.orgPos.bottom-rt.bottom;

		MoveWindow(hwnd, rt.left, rt.top, rt.right-rt.left, rt.bottom-rt.top, TRUE);
	} else {
		MoveWindow(hwnd, g_fullscreen.orgPos.left, g_fullscreen.orgPos.top,
							g_fullscreen.orgPos.right-g_fullscreen.orgPos.left,
							g_fullscreen.orgPos.bottom-g_fullscreen.orgPos.top, TRUE);

		if (g_fullscreen.wasZoomed)
			ShowWindow(hwnd, WS_MAXIMIZE);
	}

	return g_fullscreen.mode;
}

static void fullscreen_move(HWND hwnd)
{
	RECT rt, pos;
	GetWindowRect(hwnd, &pos);

	Frame_CalcFrameClient(hwnd, &rt);
	ClientToScreen(hwnd, (LPPOINT)&rt.left);
	ClientToScreen(hwnd, (LPPOINT)&rt.right);

	rt.left = pos.left-rt.left;
	rt.top = pos.top-rt.top;
	rt.right = GetSystemMetrics(SM_CXSCREEN)+pos.right-rt.right;
	rt.bottom = GetSystemMetrics(SM_CYSCREEN)+pos.bottom-rt.bottom;

	MoveWindow(hwnd, rt.left, rt.top, rt.right-rt.left, rt.bottom-rt.top, TRUE);
}

#endif


static void toggle_child(HWND hwnd, UINT cmd, HWND hchild)
{
	BOOL vis = IsWindowVisible(hchild);

	CheckMenuItem(Globals.hMenuOptions, cmd, vis?MF_BYCOMMAND:MF_BYCOMMAND|MF_CHECKED);

	ShowWindow(hchild, vis?SW_HIDE:SW_SHOW);

#ifndef _NO_EXTENSIONS
	if (g_fullscreen.mode)
		fullscreen_move(hwnd);
#endif

	resize_frame_client(hwnd);
}

Mike McCormack's avatar
Mike McCormack committed
2234
static BOOL activate_drive_window(LPCTSTR path)
2235 2236 2237 2238 2239 2240
{
	TCHAR drv1[_MAX_DRIVE], drv2[_MAX_DRIVE];
	HWND child_wnd;

	_tsplitpath(path, drv1, 0, 0, 0);

2241
	/* search for a already open window for the same drive */
2242
	for(child_wnd=GetNextWindow(Globals.hmdiclient,GW_CHILD); child_wnd; child_wnd=GetNextWindow(child_wnd, GW_HWNDNEXT)) {
2243
		ChildWnd* child = (ChildWnd*) GetWindowLongPtr(child_wnd, GWLP_USERDATA);
2244 2245 2246 2247 2248 2249 2250

		if (child) {
			_tsplitpath(child->root.path, drv2, 0, 0, 0);

			if (!lstrcmpi(drv2, drv1)) {
				SendMessage(Globals.hmdiclient, WM_MDIACTIVATE, (WPARAM)child_wnd, 0);

2251
				if (IsIconic(child_wnd))
2252 2253 2254 2255 2256 2257 2258 2259 2260 2261
					ShowWindow(child_wnd, SW_SHOWNORMAL);

				return TRUE;
			}
		}
	}

	return FALSE;
}

Mike McCormack's avatar
Mike McCormack committed
2262
static BOOL activate_fs_window(LPCTSTR filesys)
2263 2264 2265 2266 2267
{
	HWND child_wnd;

	/* search for a already open window of the given file system name */
	for(child_wnd=GetNextWindow(Globals.hmdiclient,GW_CHILD); child_wnd; child_wnd=GetNextWindow(child_wnd, GW_HWNDNEXT)) {
2268
		ChildWnd* child = (ChildWnd*) GetWindowLongPtr(child_wnd, GWLP_USERDATA);
2269 2270 2271 2272 2273

		if (child) {
			if (!lstrcmpi(child->root.fs, filesys)) {
				SendMessage(Globals.hmdiclient, WM_MDIACTIVATE, (WPARAM)child_wnd, 0);

2274
				if (IsIconic(child_wnd))
2275 2276 2277 2278 2279 2280 2281 2282 2283 2284
					ShowWindow(child_wnd, SW_SHOWNORMAL);

				return TRUE;
			}
		}
	}

	return FALSE;
}

Mike McCormack's avatar
Mike McCormack committed
2285
static LRESULT CALLBACK FrameWndProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam)
2286
{
2287
	TCHAR b1[BUFFER_LEN], b2[BUFFER_LEN];
2288

2289 2290
	switch(nmsg) {
		case WM_CLOSE:
2291
			if (Globals.saveSettings)
2292 2293
				save_registry_settings();  
			
2294
			DestroyWindow(hwnd);
2295 2296 2297 2298 2299 2300 2301 2302

			 /* clear handle variables */
			Globals.hMenuFrame = 0;
			Globals.hMenuView = 0;
			Globals.hMenuOptions = 0;
			Globals.hMainWnd = 0;
			Globals.hmdiclient = 0;
			Globals.hdrivebar = 0;
2303 2304 2305
			break;

		case WM_DESTROY:
2306
			PostQuitMessage(0);
2307 2308
			break;

2309 2310 2311 2312 2313 2314 2315
		case WM_INITMENUPOPUP: {
			HWND hwndClient = (HWND) SendMessage(Globals.hmdiclient, WM_MDIGETACTIVE, 0, 0);

			if (!SendMessage(hwndClient, WM_INITMENUPOPUP, wparam, lparam))
				return 0;
			break;}

2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342
		case WM_COMMAND: {
			UINT cmd = LOWORD(wparam);
			HWND hwndClient = (HWND) SendMessage(Globals.hmdiclient, WM_MDIGETACTIVE, 0, 0);

			if (SendMessage(hwndClient, WM_DISPATCH_COMMAND, wparam, lparam))
				break;

			if (cmd>=ID_DRIVE_FIRST && cmd<=ID_DRIVE_FIRST+0xFF) {
				TCHAR drv[_MAX_DRIVE], path[MAX_PATH];
				ChildWnd* child;
				LPCTSTR root = Globals.drives;
				int i;

				for(i=cmd-ID_DRIVE_FIRST; i--; root++)
					while(*root)
						root++;

				if (activate_drive_window(root))
					return 0;

				_tsplitpath(root, drv, 0, 0, 0);

				if (!SetCurrentDirectory(drv)) {
					display_error(hwnd, GetLastError());
					return 0;
				}

2343 2344
				GetCurrentDirectory(MAX_PATH, path); /*TODO: store last directory per drive */
				child = alloc_child_window(path, NULL, hwnd);
2345 2346

				if (!create_child_window(child))
2347
					HeapFree(GetProcessHeap(), 0, child);
2348 2349
			} else switch(cmd) {
				case ID_FILE_EXIT:
2350
					SendMessage(hwnd, WM_CLOSE, 0, 0);
2351 2352 2353 2354 2355 2356 2357
					break;

				case ID_WINDOW_NEW: {
					TCHAR path[MAX_PATH];
					ChildWnd* child;

					GetCurrentDirectory(MAX_PATH, path);
2358
					child = alloc_child_window(path, NULL, hwnd);
2359 2360

					if (!create_child_window(child))
2361
						HeapFree(GetProcessHeap(), 0, child);
2362 2363
					break;}

2364 2365 2366 2367
				case ID_REFRESH:
					refresh_drives();
					break;

2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382
				case ID_WINDOW_CASCADE:
					SendMessage(Globals.hmdiclient, WM_MDICASCADE, 0, 0);
					break;

				case ID_WINDOW_TILE_HORZ:
					SendMessage(Globals.hmdiclient, WM_MDITILE, MDITILE_HORIZONTAL, 0);
					break;

				case ID_WINDOW_TILE_VERT:
					SendMessage(Globals.hmdiclient, WM_MDITILE, MDITILE_VERTICAL, 0);
					break;

				case ID_WINDOW_ARRANGE:
					SendMessage(Globals.hmdiclient, WM_MDIICONARRANGE, 0, 0);
					break;
2383

2384 2385 2386
				case ID_SELECT_FONT:
                                        choose_font(hwnd);
                                        break;
2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399

				case ID_VIEW_TOOL_BAR:
					toggle_child(hwnd, cmd, Globals.htoolbar);
					break;

				case ID_VIEW_DRIVE_BAR:
					toggle_child(hwnd, cmd, Globals.hdrivebar);
					break;

				case ID_VIEW_STATUSBAR:
					toggle_child(hwnd, cmd, Globals.hstatusbar);
					break;

2400 2401 2402
				case ID_VIEW_SAVESETTINGS:
					Globals.saveSettings = !Globals.saveSettings;
					CheckMenuItem(Globals.hMenuOptions, ID_VIEW_SAVESETTINGS,
2403
                                                      Globals.saveSettings ? MF_CHECKED : MF_UNCHECKED );
2404 2405
					break;

2406
				case ID_EXECUTE: {
2407 2408 2409 2410
					struct ExecuteDialog dlg;

					memset(&dlg, 0, sizeof(struct ExecuteDialog));

2411
					if (DialogBoxParam(Globals.hInstance, MAKEINTRESOURCE(IDD_EXECUTE), hwnd, ExecuteDialogDlgProc, (LPARAM)&dlg) == IDOK) {
2412 2413 2414 2415 2416
						HINSTANCE hinst = ShellExecute(hwnd, NULL/*operation*/, dlg.cmd/*file*/, NULL/*parameters*/, NULL/*dir*/, dlg.cmdshow);

						if ((int)hinst <= 32)
							display_error(hwnd, GetLastError());
					}
2417 2418
					break;}

2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442
				case ID_CONNECT_NETWORK_DRIVE: {
					DWORD ret = WNetConnectionDialog(hwnd, RESOURCETYPE_DISK);
					if (ret == NO_ERROR)
						refresh_drives();
					else if (ret != (DWORD)-1) {
						if (ret == ERROR_EXTENDED_ERROR)
							display_network_error(hwnd);
						else
							display_error(hwnd, ret);
					}
					break;}

				case ID_DISCONNECT_NETWORK_DRIVE: {
					DWORD ret = WNetDisconnectDialog(hwnd, RESOURCETYPE_DISK);
					if (ret == NO_ERROR)
						refresh_drives();
					else if (ret != (DWORD)-1) {
						if (ret == ERROR_EXTENDED_ERROR)
							display_network_error(hwnd);
						else
							display_error(hwnd, ret);
					}
					break;}

2443 2444 2445 2446 2447 2448 2449
				case ID_FORMAT_DISK: {
					UINT sem_org = SetErrorMode(0); /* Get the current Error Mode settings. */
					SetErrorMode(sem_org & ~SEM_FAILCRITICALERRORS); /* Force O/S to handle */
					SHFormatDrive(hwnd, 0 /* A: */, SHFMT_ID_DEFAULT, 0);
					SetErrorMode(sem_org); /* Put it back the way it was. */
					break;}

2450
				case ID_HELP:
2451
					WinHelp(hwnd, RS(b1,IDS_WINEFILE), HELP_INDEX, 0);
2452 2453 2454 2455 2456 2457 2458
					break;

#ifndef _NO_EXTENSIONS
				case ID_VIEW_FULLSCREEN:
					CheckMenuItem(Globals.hMenuOptions, cmd, toggle_fullscreen(hwnd)?MF_CHECKED:0);
					break;

2459
#ifdef __WINE__
2460 2461
				case ID_DRIVE_UNIX_FS: {
					TCHAR path[MAX_PATH];
2462 2463 2464
#ifdef UNICODE
					char cpath[MAX_PATH];
#endif
2465 2466
					ChildWnd* child;

2467
					if (activate_fs_window(RS(b1,IDS_UNIXFS)))
2468 2469
						break;

2470 2471 2472 2473
#ifdef UNICODE
					getcwd(cpath, MAX_PATH);
					MultiByteToWideChar(CP_UNIXCP, 0, cpath, -1, path, MAX_PATH);
#else
2474
					getcwd(path, MAX_PATH);
2475
#endif
2476 2477 2478
					child = alloc_child_window(path, NULL, hwnd);

					if (!create_child_window(child))
2479
						HeapFree(GetProcessHeap(), 0, child);
2480 2481 2482 2483 2484 2485 2486
					break;}
#endif
#ifdef _SHELL_FOLDERS
				case ID_DRIVE_SHELL_NS: {
					TCHAR path[MAX_PATH];
					ChildWnd* child;

2487
					if (activate_fs_window(RS(b1,IDS_SHELL)))
2488 2489 2490 2491
						break;

					GetCurrentDirectory(MAX_PATH, path);
					child = alloc_child_window(path, get_path_pidl(path,hwnd), hwnd);
2492 2493

					if (!create_child_window(child))
2494
						HeapFree(GetProcessHeap(), 0, child);
2495 2496 2497 2498
					break;}
#endif
#endif

2499
				/*TODO: There are even more menu items! */
2500 2501

#ifndef _NO_EXTENSIONS
2502
#ifdef __WINE__
2503 2504 2505 2506 2507 2508 2509 2510 2511
				case ID_LICENSE:
					WineLicense(Globals.hMainWnd);
					break;

				case ID_NO_WARRANTY:
					WineWarranty(Globals.hMainWnd);
					break;

				case ID_ABOUT_WINE:
2512
					ShellAbout(hwnd, RS(b2,IDS_WINE), RS(b1,IDS_WINEFILE), 0);
2513
					break;
2514
#endif
2515

2516
				case ID_ABOUT:
2517
					ShellAbout(hwnd, RS(b1,IDS_WINEFILE), NULL, 0);
2518 2519
					break;
#endif	/* _NO_EXTENSIONS */
2520 2521

				default:
2522
					/*TODO: if (wParam >= PM_FIRST_LANGUAGE && wParam <= PM_LAST_LANGUAGE)
2523 2524 2525
						STRING_SelectLanguageByNumber(wParam - PM_FIRST_LANGUAGE);
					else */if ((cmd<IDW_FIRST_CHILD || cmd>=IDW_FIRST_CHILD+0x100) &&
						(cmd<SC_SIZE || cmd>SC_RESTORE))
2526
						MessageBox(hwnd, RS(b2,IDS_NO_IMPL), RS(b1,IDS_WINEFILE), MB_OK);
2527 2528 2529 2530 2531 2532 2533

					return DefFrameProc(hwnd, Globals.hmdiclient, nmsg, wparam, lparam);
			}
			break;}

		case WM_SIZE:
			resize_frame(hwnd, LOWORD(lparam), HIWORD(lparam));
2534
			break;	/* do not pass message to DefFrameProc */
2535

2536 2537 2538 2539
		case WM_DEVICECHANGE:
			SendMessage(hwnd, WM_COMMAND, MAKELONG(ID_REFRESH,0), 0);
			break;

2540 2541 2542 2543
#ifndef _NO_EXTENSIONS
		case WM_GETMINMAXINFO: {
			LPMINMAXINFO lpmmi = (LPMINMAXINFO)lparam;

2544 2545
			lpmmi->ptMaxTrackSize.x <<= 1;/*2*GetSystemMetrics(SM_CXSCREEN) / SM_CXVIRTUALSCREEN */
			lpmmi->ptMaxTrackSize.y <<= 1;/*2*GetSystemMetrics(SM_CYSCREEN) / SM_CYVIRTUALSCREEN */
2546 2547 2548 2549 2550
			break;}

		case FRM_CALC_CLIENT:
			frame_get_clientspace(hwnd, (PRECT)lparam);
			return TRUE;
2551
#endif /* _NO_EXTENSIONS */
2552 2553 2554 2555 2556 2557 2558 2559 2560

		default:
			return DefFrameProc(hwnd, Globals.hmdiclient, nmsg, wparam, lparam);
	}

	return 0;
}


2561 2562
static TCHAR g_pos_names[COLUMNS][20] = {
	{'\0'}	/* symbol */
2563 2564
};

2565
static const int g_pos_align[] = {
2566
	0,
2567 2568 2569
	HDF_LEFT,	/* Name */
	HDF_RIGHT,	/* Size */
	HDF_LEFT,	/* CDate */
2570
#ifndef _NO_EXTENSIONS
2571 2572 2573 2574
	HDF_LEFT,	/* ADate */
	HDF_LEFT,	/* MDate */
	HDF_LEFT,	/* Index */
	HDF_CENTER,	/* Links */
2575
#endif
2576
	HDF_CENTER,	/* Attributes */
2577
#ifndef _NO_EXTENSIONS
2578
	HDF_LEFT	/* Security */
2579 2580 2581 2582 2583 2584
#endif
};

static void resize_tree(ChildWnd* child, int cx, int cy)
{
	HDWP hdwp = BeginDeferWindowPos(4);
2585 2586 2587 2588 2589 2590
	RECT rt;

	rt.left   = 0;
	rt.top    = 0;
	rt.right  = cx;
	rt.bottom = cy;
2591 2592 2593 2594 2595 2596

	cx = child->split_pos + SPLIT_WIDTH/2;

#ifndef _NO_EXTENSIONS
	{
		WINDOWPOS wp;
2597 2598 2599 2600
		HD_LAYOUT hdl;

		hdl.prc   = &rt;
		hdl.pwpos = &wp;
2601

2602
		SendMessage(child->left.hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hdl);
2603 2604 2605 2606 2607 2608

		DeferWindowPos(hdwp, child->left.hwndHeader, wp.hwndInsertAfter,
						wp.x-1, wp.y, child->split_pos-SPLIT_WIDTH/2+1, wp.cy, wp.flags);
		DeferWindowPos(hdwp, child->right.hwndHeader, wp.hwndInsertAfter,
						rt.left+cx+1, wp.y, wp.cx-cx+2, wp.cy, wp.flags);
	}
2609
#endif /* _NO_EXTENSIONS */
2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621

	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);
	DeferWindowPos(hdwp, child->right.hwnd, 0, rt.left+cx+1, rt.top, rt.right-cx, rt.bottom-rt.top, SWP_NOZORDER|SWP_NOACTIVATE);

	EndDeferWindowPos(hdwp);
}


#ifndef _NO_EXTENSIONS

static HWND create_header(HWND parent, Pane* pane, int id)
{
2622
	HD_ITEM hdi;
2623 2624
	int idx;

2625
	HWND hwnd = CreateWindow(WC_HEADER, 0, WS_CHILD|WS_VISIBLE|HDS_HORZ|HDS_FULLDRAG/*TODO: |HDS_BUTTONS + sort orders*/,
2626 2627 2628 2629
								0, 0, 0, 0, parent, (HMENU)id, Globals.hInstance, 0);
	if (!hwnd)
		return 0;

2630
	SendMessage(hwnd, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), FALSE);
2631

2632 2633
	hdi.mask = HDI_TEXT|HDI_WIDTH|HDI_FORMAT;

2634 2635 2636 2637
	for(idx=0; idx<COLUMNS; idx++) {
		hdi.pszText = g_pos_names[idx];
		hdi.fmt = HDF_STRING | g_pos_align[idx];
		hdi.cxy = pane->widths[idx];
2638
		SendMessage(hwnd, HDM_INSERTITEM, idx, (LPARAM) &hdi);
2639 2640 2641 2642 2643
	}

	return hwnd;
}

2644
#endif /* _NO_EXTENSIONS */
2645 2646 2647 2648


static void init_output(HWND hwnd)
{
2649 2650
	static const WCHAR s1000[] = {'1','0','0','0','\0'};
	WCHAR b[16];
2651 2652 2653
	HFONT old_font;
	HDC hdc = GetDC(hwnd);

2654
	if (GetNumberFormatW(LOCALE_USER_DEFAULT, 0, s1000, 0, b, 16) > 4)
2655 2656
		Globals.num_sep = b[1];
	else
2657
		Globals.num_sep = '.';
2658

2659
	old_font = SelectObject(hdc, Globals.hfont);
2660
	GetTextExtentPoint32W(hdc, sSpace, 1, &Globals.spaceSize);
2661
	SelectObject(hdc, old_font);
2662 2663 2664 2665 2666 2667
	ReleaseDC(hwnd, hdc);
}

static void draw_item(Pane* pane, LPDRAWITEMSTRUCT dis, Entry* entry, int calcWidthCol);


2668
/* calculate preferred width for all visible columns */
2669 2670 2671 2672

static BOOL calc_widths(Pane* pane, BOOL anyway)
{
	int col, x, cx, spc=3*Globals.spaceSize.cx;
2673
	int entries = SendMessage(pane->hwnd, LB_GETCOUNT, 0, 0);
2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688
	int orgWidths[COLUMNS];
	int orgPositions[COLUMNS+1];
	HFONT hfontOld;
	HDC hdc;
	int cnt;

	if (!anyway) {
		memcpy(orgWidths, pane->widths, sizeof(orgWidths));
		memcpy(orgPositions, pane->positions, sizeof(orgPositions));
	}

	for(col=0; col<COLUMNS; col++)
		pane->widths[col] = 0;

	hdc = GetDC(pane->hwnd);
2689
	hfontOld = SelectObject(hdc, Globals.hfont);
2690 2691

	for(cnt=0; cnt<entries; cnt++) {
2692
		Entry* entry = (Entry*) SendMessage(pane->hwnd, LB_GETITEMDATA, cnt, 0);
2693

2694 2695
		DRAWITEMSTRUCT dis;

2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707
		dis.CtlType		  = 0;
		dis.CtlID		  = 0;
		dis.itemID		  = 0;
		dis.itemAction	  = 0;
		dis.itemState	  = 0;
		dis.hwndItem	  = pane->hwnd;
		dis.hDC			  = hdc;
		dis.rcItem.left	  = 0;
		dis.rcItem.top    = 0;
		dis.rcItem.right  = 0;
		dis.rcItem.bottom = 0;
		/*dis.itemData	  = 0; */
2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733

		draw_item(pane, &dis, entry, COLUMNS);
	}

	SelectObject(hdc, hfontOld);
	ReleaseDC(pane->hwnd, hdc);

	x = 0;
	for(col=0; col<COLUMNS; col++) {
		pane->positions[col] = x;
		cx = pane->widths[col];

		if (cx) {
			cx += spc;

			if (cx < IMAGE_WIDTH)
				cx = IMAGE_WIDTH;

			pane->widths[col] = cx;
		}

		x += cx;
	}

	pane->positions[COLUMNS] = x;

2734
	SendMessage(pane->hwnd, LB_SETHORIZONTALEXTENT, x, 0);
2735

2736
	/* no change? */
2737 2738 2739
	if (!memcmp(orgWidths, pane->widths, sizeof(orgWidths)))
		return FALSE;

2740
	/* don't move, if only collapsing an entry */
2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754
	if (!anyway && pane->widths[0]<orgWidths[0] &&
		!memcmp(orgWidths+1, pane->widths+1, sizeof(orgWidths)-sizeof(int))) {
		pane->widths[0] = orgWidths[0];
		memcpy(pane->positions, orgPositions, sizeof(orgPositions));

		return FALSE;
	}

	InvalidateRect(pane->hwnd, 0, TRUE);

	return TRUE;
}


2755
/* calculate one preferred column width */
2756 2757 2758 2759 2760

static void calc_single_width(Pane* pane, int col)
{
	HFONT hfontOld;
	int x, cx;
2761
	int entries = SendMessage(pane->hwnd, LB_GETCOUNT, 0, 0);
2762 2763 2764 2765 2766 2767
	int cnt;
	HDC hdc;

	pane->widths[col] = 0;

	hdc = GetDC(pane->hwnd);
2768
	hfontOld = SelectObject(hdc, Globals.hfont);
2769 2770

	for(cnt=0; cnt<entries; cnt++) {
2771
		Entry* entry = (Entry*) SendMessage(pane->hwnd, LB_GETITEMDATA, cnt, 0);
2772 2773
		DRAWITEMSTRUCT dis;

2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785
		dis.CtlType		  = 0;
		dis.CtlID		  = 0;
		dis.itemID		  = 0;
		dis.itemAction	  = 0;
		dis.itemState	  = 0;
		dis.hwndItem	  = pane->hwnd;
		dis.hDC			  = hdc;
		dis.rcItem.left	  = 0;
		dis.rcItem.top    = 0;
		dis.rcItem.right  = 0;
		dis.rcItem.bottom = 0;
		/*dis.itemData	  = 0; */
2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810

		draw_item(pane, &dis, entry, col);
	}

	SelectObject(hdc, hfontOld);
	ReleaseDC(pane->hwnd, hdc);

	cx = pane->widths[col];

	if (cx) {
		cx += 3*Globals.spaceSize.cx;

		if (cx < IMAGE_WIDTH)
			cx = IMAGE_WIDTH;
	}

	pane->widths[col] = cx;

	x = pane->positions[col] + cx;

	for(; col<COLUMNS; ) {
		pane->positions[++col] = x;
		x += pane->widths[col];
	}

2811
	SendMessage(pane->hwnd, LB_SETHORIZONTALEXTENT, x, 0);
2812 2813 2814
}


2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825
static BOOL pattern_match(LPCTSTR str, LPCTSTR pattern)
{
	for( ; *str&&*pattern; str++,pattern++) {
		if (*pattern == '*') {
			do pattern++;
			while(*pattern == '*');

			if (!*pattern)
				return TRUE;

			for(; *str; str++)
2826
				if (*str==*pattern && pattern_match(str, pattern))
2827 2828 2829 2830
					return TRUE;

			return FALSE;
		}
2831
		else if (*str!=*pattern && *pattern!='?')
2832 2833 2834 2835 2836 2837 2838 2839 2840 2841
			return FALSE;
	}

	if (*str || *pattern)
		if (*pattern!='*' || pattern[1]!='\0')
			return FALSE;

	return TRUE;
}

2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853
static BOOL pattern_imatch(LPCTSTR str, LPCTSTR pattern)
{
	TCHAR b1[BUFFER_LEN], b2[BUFFER_LEN];

	lstrcpy(b1, str);
	lstrcpy(b2, pattern);
	CharUpper(b1);
	CharUpper(b2);

	return pattern_match(b1, b2);
}

2854 2855 2856 2857 2858 2859 2860 2861 2862 2863

enum FILE_TYPE {
	FT_OTHER		= 0,
	FT_EXECUTABLE	= 1,
	FT_DOCUMENT		= 2
};

static enum FILE_TYPE get_file_type(LPCTSTR filename);


2864
/* insert listbox entries after index idx */
2865

2866
static int insert_entries(Pane* pane, Entry* dir, LPCTSTR pattern, int filter_flags, int idx)
2867
{
2868
	Entry* entry = dir;
2869 2870

	if (!entry)
2871
		return idx;
2872 2873 2874 2875 2876 2877 2878 2879 2880

	ShowWindow(pane->hwnd, SW_HIDE);

	for(; entry; entry=entry->next) {
#ifndef _LEFT_FILES
		if (pane->treePane && !(entry->data.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY))
			continue;
#endif

2881 2882
		if (entry->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
			/* don't display entries "." and ".." in the left pane */
2883
			if (pane->treePane && entry->data.cFileName[0] == '.')
2884
				if (
2885
#ifndef _NO_EXTENSIONS
2886
					entry->data.cFileName[1] == '\0' ||
2887
#endif
2888
					(entry->data.cFileName[1] == '.' && entry->data.cFileName[2] == '\0'))
2889 2890 2891 2892
					continue;

			/* filter directories in right pane */
			if (!pane->treePane && !(filter_flags&TF_DIRECTORIES))
2893
				continue;
2894 2895 2896 2897
		}

		/* filter using the file name pattern */
		if (pattern)
2898
			if (!pattern_imatch(entry->data.cFileName, pattern))
2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921
				continue;

		/* filter system and hidden files */
		if (!(filter_flags&TF_HIDDEN) && (entry->data.dwFileAttributes&(FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM)))
			continue;

		/* filter looking at the file type */
		if ((filter_flags&(TF_PROGRAMS|TF_DOCUMENTS|TF_OTHERS)) != (TF_PROGRAMS|TF_DOCUMENTS|TF_OTHERS))
			switch(get_file_type(entry->data.cFileName)) {
			  case FT_EXECUTABLE:
			  	if (!(filter_flags & TF_PROGRAMS))
					continue;
				break;

			  case FT_DOCUMENT:
				if (!(filter_flags & TF_DOCUMENTS))
					continue;
				break;

			  default: /* TF_OTHERS */
				if (!(filter_flags & TF_OTHERS))
					continue;
			}
2922 2923 2924 2925

		if (idx != -1)
			idx++;

2926
		SendMessage(pane->hwnd, LB_INSERTSTRING, idx, (LPARAM) entry);
2927

2928
		if (pane->treePane && entry->expanded)
2929
			idx = insert_entries(pane, entry->down, pattern, filter_flags, idx);
2930 2931 2932
	}

	ShowWindow(pane->hwnd, SW_SHOW);
2933 2934

	return idx;
2935 2936 2937
}


2938 2939
static void format_bytes(LPTSTR buffer, LONGLONG bytes)
{
2940 2941 2942
	static const TCHAR sFmtGB[] = {'%', '.', '1', 'f', ' ', 'G', 'B', '\0'};
	static const TCHAR sFmtMB[] = {'%', '.', '1', 'f', ' ', 'M', 'B', '\0'};
	static const TCHAR sFmtkB[] = {'%', '.', '1', 'f', ' ', 'k', 'B', '\0'};
2943 2944 2945 2946

	float fBytes = (float)bytes;

	if (bytes >= 1073741824)	/* 1 GB */
2947
		_stprintf(buffer, sFmtGB, fBytes/1073741824.f+.5f);
2948
	else if (bytes >= 1048576)	/* 1 MB */
2949
		_stprintf(buffer, sFmtMB, fBytes/1048576.f+.5f);
2950
	else if (bytes >= 1024)		/* 1 kB */
2951
		_stprintf(buffer, sFmtkB, fBytes/1024.f+.5f);
2952 2953 2954 2955
	else
		_stprintf(buffer, sLongNumFmt, bytes);
}

Mike McCormack's avatar
Mike McCormack committed
2956
static void set_space_status(void)
2957 2958 2959 2960 2961 2962 2963
{
	ULARGE_INTEGER ulFreeBytesToCaller, ulTotalBytes, ulFreeBytes;
	TCHAR fmt[64], b1[64], b2[64], buffer[BUFFER_LEN];

	if (GetDiskFreeSpaceEx(NULL, &ulFreeBytesToCaller, &ulTotalBytes, &ulFreeBytes)) {
		format_bytes(b1, ulFreeBytesToCaller.QuadPart);
		format_bytes(b2, ulTotalBytes.QuadPart);
2964
		wsprintf(buffer, RS(fmt,IDS_FREE_SPACE_FMT), b1, b2);
2965
	} else
2966
		lstrcpy(buffer, sQMarks);
2967 2968 2969 2970 2971

	SendMessage(Globals.hstatusbar, SB_SETTEXT, 0, (LPARAM)buffer);
}


2972 2973
static WNDPROC g_orgTreeWndProc;

2974
static void create_tree_window(HWND parent, Pane* pane, int id, int id_header, LPCTSTR pattern, int filter_flags)
2975
{
2976
	static const TCHAR sListBox[] = {'L','i','s','t','B','o','x','\0'};
2977

2978 2979 2980
	static int s_init = 0;
	Entry* entry = pane->root;

2981
	pane->hwnd = CreateWindow(sListBox, sEmpty, WS_CHILD|WS_VISIBLE|WS_HSCROLL|WS_VSCROLL|
2982 2983 2984
								LBS_DISABLENOSCROLL|LBS_NOINTEGRALHEIGHT|LBS_OWNERDRAWFIXED|LBS_NOTIFY,
								0, 0, 0, 0, parent, (HMENU)id, Globals.hInstance, 0);

2985
	SetWindowLongPtr(pane->hwnd, GWLP_USERDATA, (LPARAM)pane);
2986
	g_orgTreeWndProc = (WNDPROC) SetWindowLongPtr(pane->hwnd, GWLP_WNDPROC, (LPARAM)TreeWndProc);
2987

2988
	SendMessage(pane->hwnd, WM_SETFONT, (WPARAM)Globals.hfont, FALSE);
2989

2990
	/* insert entries into listbox */
2991
	if (entry)
2992
		insert_entries(pane, entry, pattern, filter_flags, -1);
2993

2994
	/* calculate column widths */
2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009
	if (!s_init) {
		s_init = 1;
		init_output(pane->hwnd);
	}

	calc_widths(pane, TRUE);

#ifndef _NO_EXTENSIONS
	pane->hwndHeader = create_header(parent, pane, id_header);
#endif
}


static void InitChildWindow(ChildWnd* child)
{
3010 3011
	create_tree_window(child->hwnd, &child->left, IDW_TREE_LEFT, IDW_HEADER_LEFT, NULL, TF_ALL);
	create_tree_window(child->hwnd, &child->right, IDW_TREE_RIGHT, IDW_HEADER_RIGHT, child->filter_pattern, child->filter_flags);
3012 3013 3014 3015 3016 3017 3018 3019 3020
}


static void format_date(const FILETIME* ft, TCHAR* buffer, int visible_cols)
{
	SYSTEMTIME systime;
	FILETIME lft;
	int len = 0;

3021
	*buffer = '\0';
3022 3023 3024 3025 3026

	if (!ft->dwLowDateTime && !ft->dwHighDateTime)
		return;

	if (!FileTimeToLocalFileTime(ft, &lft))
3027
		{err: lstrcpy(buffer,sQMarks); return;}
3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044

	if (!FileTimeToSystemTime(&lft, &systime))
		goto err;

	if (visible_cols & COL_DATE) {
		len = GetDateFormat(LOCALE_USER_DEFAULT, 0, &systime, 0, buffer, BUFFER_LEN);
		if (!len)
			goto err;
	}

	if (visible_cols & COL_TIME) {
		if (len)
			buffer[len-1] = ' ';

		buffer[len++] = ' ';

		if (!GetTimeFormat(LOCALE_USER_DEFAULT, 0, &systime, 0, buffer+len, BUFFER_LEN-len))
3045
			buffer[len] = '\0';
3046 3047 3048 3049 3050 3051
	}
}


static void calc_width(Pane* pane, LPDRAWITEMSTRUCT dis, int col, LPCTSTR str)
{
3052
	RECT rt = {0, 0, 0, 0};
3053

3054
	DrawText(dis->hDC, str, -1, &rt, DT_CALCRECT|DT_SINGLELINE|DT_NOPREFIX);
3055 3056 3057 3058 3059 3060 3061

	if (rt.right > pane->widths[col])
		pane->widths[col] = rt.right;
}

static void calc_tabbed_width(Pane* pane, LPDRAWITEMSTRUCT dis, int col, LPCTSTR str)
{
3062
	RECT rt = {0, 0, 0, 0};
3063 3064 3065 3066

/*	DRAWTEXTPARAMS dtp = {sizeof(DRAWTEXTPARAMS), 2};
	DrawTextEx(dis->hDC, (LPTSTR)str, -1, &rt, DT_CALCRECT|DT_SINGLELINE|DT_NOPREFIX|DT_EXPANDTABS|DT_TABSTOP, &dtp);*/

3067
	DrawText(dis->hDC, str, -1, &rt, DT_CALCRECT|DT_SINGLELINE|DT_EXPANDTABS|DT_TABSTOP|(2<<8));
3068
	/*FIXME rt (0,0) ??? */
3069 3070 3071 3072 3073 3074 3075 3076 3077

	if (rt.right > pane->widths[col])
		pane->widths[col] = rt.right;
}


static void output_text(Pane* pane, LPDRAWITEMSTRUCT dis, int col, LPCTSTR str, DWORD flags)
{
	int x = dis->rcItem.left;
3078 3079 3080 3081 3082 3083
	RECT rt;

	rt.left   = x+pane->positions[col]+Globals.spaceSize.cx;
	rt.top    = dis->rcItem.top;
	rt.right  = x+pane->positions[col+1]-Globals.spaceSize.cx;
	rt.bottom = dis->rcItem.bottom;
3084

3085
	DrawText(dis->hDC, str, -1, &rt, DT_SINGLELINE|DT_NOPREFIX|flags);
3086 3087 3088 3089 3090
}

static void output_tabbed_text(Pane* pane, LPDRAWITEMSTRUCT dis, int col, LPCTSTR str)
{
	int x = dis->rcItem.left;
3091 3092 3093 3094 3095 3096
	RECT rt;

	rt.left   = x+pane->positions[col]+Globals.spaceSize.cx;
	rt.top    = dis->rcItem.top;
	rt.right  = x+pane->positions[col+1]-Globals.spaceSize.cx;
	rt.bottom = dis->rcItem.bottom;
3097 3098 3099 3100

/*	DRAWTEXTPARAMS dtp = {sizeof(DRAWTEXTPARAMS), 2};
	DrawTextEx(dis->hDC, (LPTSTR)str, -1, &rt, DT_SINGLELINE|DT_NOPREFIX|DT_EXPANDTABS|DT_TABSTOP, &dtp);*/

3101
	DrawText(dis->hDC, str, -1, &rt, DT_SINGLELINE|DT_EXPANDTABS|DT_TABSTOP|(2<<8));
3102 3103 3104 3105 3106
}

static void output_number(Pane* pane, LPDRAWITEMSTRUCT dis, int col, LPCTSTR str)
{
	int x = dis->rcItem.left;
3107
	RECT rt;
3108 3109 3110 3111 3112
	LPCTSTR s = str;
	TCHAR b[128];
	LPTSTR d = b;
	int pos;

3113 3114 3115 3116 3117
	rt.left   = x+pane->positions[col]+Globals.spaceSize.cx;
	rt.top    = dis->rcItem.top;
	rt.right  = x+pane->positions[col+1]-Globals.spaceSize.cx;
	rt.bottom = dis->rcItem.bottom;

3118 3119 3120
	if (*s)
		*d++ = *s++;

3121
	/* insert number separator characters */
3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135
	pos = lstrlen(s) % 3;

	while(*s)
		if (pos--)
			*d++ = *s++;
		else {
			*d++ = Globals.num_sep;
			pos = 3;
		}

	DrawText(dis->hDC, b, d-b, &rt, DT_RIGHT|DT_SINGLELINE|DT_NOPREFIX|DT_END_ELLIPSIS);
}


3136
static BOOL is_exe_file(LPCTSTR ext)
3137
{
3138 3139 3140 3141 3142
	static const TCHAR executable_extensions[][4] = {
		{'C','O','M','\0'},
		{'E','X','E','\0'},
		{'B','A','T','\0'},
		{'C','M','D','\0'},
3143
#ifndef _NO_EXTENSIONS
3144 3145 3146
		{'C','M','M','\0'},
		{'B','T','M','\0'},
		{'A','W','K','\0'},
3147
#endif /* _NO_EXTENSIONS */
3148
		{'\0'}
3149 3150 3151
	};

	TCHAR ext_buffer[_MAX_EXT];
3152
	const TCHAR (*p)[4];
3153 3154 3155 3156 3157 3158
	LPCTSTR s;
	LPTSTR d;

	for(s=ext+1,d=ext_buffer; (*d=tolower(*s)); s++)
		d++;

3159
	for(p=executable_extensions; (*p)[0]; p++)
3160
		if (!lstrcmpi(ext_buffer, *p))
3161
			return TRUE;
3162

3163
	return FALSE;
3164 3165
}

3166
static BOOL is_registered_type(LPCTSTR ext)
3167
{
3168 3169 3170 3171 3172 3173
	/* check if there exists a classname for this file extension in the registry */
	if (!RegQueryValue(HKEY_CLASSES_ROOT, ext, NULL, NULL))
		return TRUE;

	return FALSE;
}
3174

3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186
static enum FILE_TYPE get_file_type(LPCTSTR filename)
{
	LPCTSTR ext = _tcsrchr(filename, '.');
	if (!ext)
		ext = sEmpty;

	if (is_exe_file(ext))
		return FT_EXECUTABLE;
	else if (is_registered_type(ext))
		return FT_DOCUMENT;
	else
		return FT_OTHER;
3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205
}


static void draw_item(Pane* pane, LPDRAWITEMSTRUCT dis, Entry* entry, int calcWidthCol)
{
	TCHAR buffer[BUFFER_LEN];
	DWORD attrs;
	int visible_cols = pane->visible_cols;
	COLORREF bkcolor, textcolor;
	RECT focusRect = dis->rcItem;
	HBRUSH hbrush;
	enum IMAGE img;
	int img_pos, cx;
	int col = 0;

	if (entry) {
		attrs = entry->data.dwFileAttributes;

		if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
3206 3207
			if (entry->data.cFileName[0] == '.' && entry->data.cFileName[1] == '.'
					&& entry->data.cFileName[2] == '\0')
3208 3209
				img = IMG_FOLDER_UP;
#ifndef _NO_EXTENSIONS
3210
			else if (entry->data.cFileName[0] == '.' && entry->data.cFileName[1] == '\0')
3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221
				img = IMG_FOLDER_CUR;
#endif
			else if (
#ifdef _NO_EXTENSIONS
					 entry->expanded ||
#endif
					 (pane->treePane && (dis->itemState&ODS_FOCUS)))
				img = IMG_OPEN_FOLDER;
			else
				img = IMG_FOLDER;
		} else {
3222 3223 3224 3225 3226
			switch(get_file_type(entry->data.cFileName)) {
			  case FT_EXECUTABLE:	img = IMG_EXECUTABLE;	break;
			  case FT_DOCUMENT:		img = IMG_DOCUMENT;		break;
			  default:				img = IMG_FILE;
			}
3227 3228 3229 3230 3231 3232 3233 3234
		}
	} else {
		attrs = 0;
		img = IMG_NONE;
	}

	if (pane->treePane) {
		if (entry) {
Piotr Caban's avatar
Piotr Caban committed
3235
			img_pos = dis->rcItem.left + entry->level*(IMAGE_WIDTH+TREE_LINE_DX);
3236 3237 3238 3239 3240

			if (calcWidthCol == -1) {
				int x;
				int y = dis->rcItem.top + IMAGE_HEIGHT/2;
				Entry* up;
3241
				RECT rt_clip;
3242
				HRGN hrgn_org = CreateRectRgn(0, 0, 0, 0);
3243 3244 3245 3246 3247 3248 3249 3250
				HRGN hrgn;

				rt_clip.left   = dis->rcItem.left;
				rt_clip.top    = dis->rcItem.top;
				rt_clip.right  = dis->rcItem.left+pane->widths[col];
				rt_clip.bottom = dis->rcItem.bottom;

				hrgn = CreateRectRgnIndirect(&rt_clip);
3251 3252 3253 3254 3255 3256

				if (!GetClipRgn(dis->hDC, hrgn_org)) {
					DeleteObject(hrgn_org);
					hrgn_org = 0;
				}

3257
				/* HGDIOBJ holdPen = SelectObject(dis->hDC, GetStockObject(BLACK_PEN)); */
3258 3259 3260 3261 3262 3263 3264 3265 3266 3267
				ExtSelectClipRgn(dis->hDC, hrgn, RGN_AND);
				DeleteObject(hrgn);

				if ((up=entry->up) != NULL) {
					MoveToEx(dis->hDC, img_pos-IMAGE_WIDTH/2, y, 0);
					LineTo(dis->hDC, img_pos-2, y);

					x = img_pos - IMAGE_WIDTH/2;

					do {
Piotr Caban's avatar
Piotr Caban committed
3268
						x -= IMAGE_WIDTH+TREE_LINE_DX;
3269 3270 3271

						if (up->next
#ifndef _LEFT_FILES
3272
							&& (up->next->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294
#endif
							) {
							MoveToEx(dis->hDC, x, dis->rcItem.top, 0);
							LineTo(dis->hDC, x, dis->rcItem.bottom);
						}
					} while((up=up->up) != NULL);
				}

				x = img_pos - IMAGE_WIDTH/2;

				MoveToEx(dis->hDC, x, dis->rcItem.top, 0);
				LineTo(dis->hDC, x, y);

				if (entry->next
#ifndef _LEFT_FILES
					&& (entry->next->data.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
#endif
					)
					LineTo(dis->hDC, x, dis->rcItem.bottom);

				SelectClipRgn(dis->hDC, hrgn_org);
				if (hrgn_org) DeleteObject(hrgn_org);
3295
				/* SelectObject(dis->hDC, holdPen); */
3296
			} else if (calcWidthCol==col || calcWidthCol==COLUMNS) {
Piotr Caban's avatar
Piotr Caban committed
3297
				int right = img_pos + IMAGE_WIDTH - TREE_LINE_DX;
3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320

				if (right > pane->widths[col])
					pane->widths[col] = right;
			}
		} else  {
			img_pos = dis->rcItem.left;
		}
	} else {
		img_pos = dis->rcItem.left;

		if (calcWidthCol==col || calcWidthCol==COLUMNS)
			pane->widths[col] = IMAGE_WIDTH;
	}

	if (calcWidthCol == -1) {
		focusRect.left = img_pos -2;

#ifdef _NO_EXTENSIONS
		if (pane->treePane && entry) {
			RECT rt = {0};

			DrawText(dis->hDC, entry->data.cFileName, -1, &rt, DT_CALCRECT|DT_SINGLELINE|DT_NOPREFIX);

Piotr Caban's avatar
Piotr Caban committed
3321
			focusRect.right = dis->rcItem.left+pane->positions[col+1]+TREE_LINE_DX + rt.right +2;
3322 3323 3324 3325 3326 3327
		}
#else

		if (attrs & FILE_ATTRIBUTE_COMPRESSED)
			textcolor = COLOR_COMPRESSED;
		else
3328
#endif /* _NO_EXTENSIONS */
3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350
			textcolor = RGB(0,0,0);

		if (dis->itemState & ODS_FOCUS) {
			textcolor = RGB(255,255,255);
			bkcolor = COLOR_SELECTION;
		} else {
			bkcolor = RGB(255,255,255);
		}

		hbrush = CreateSolidBrush(bkcolor);
		FillRect(dis->hDC, &focusRect, hbrush);
		DeleteObject(hbrush);

		SetBkMode(dis->hDC, TRANSPARENT);
		SetTextColor(dis->hDC, textcolor);

		cx = pane->widths[col];

		if (cx && img!=IMG_NONE) {
			if (cx > IMAGE_WIDTH)
				cx = IMAGE_WIDTH;

3351 3352 3353 3354 3355 3356 3357 3358
#ifdef _SHELL_FOLDERS
			if (entry->hicon && entry->hicon!=(HICON)-1)
				DrawIconEx(dis->hDC, img_pos, dis->rcItem.top, entry->hicon, cx, GetSystemMetrics(SM_CYSMICON), 0, 0, DI_NORMAL);
			else
#endif
				ImageList_DrawEx(Globals.himl, img, dis->hDC,
								 img_pos, dis->rcItem.top, cx,
								 IMAGE_HEIGHT, bkcolor, CLR_DEFAULT, ILD_NORMAL);
3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371
		}
	}

	if (!entry)
		return;

#ifdef _NO_EXTENSIONS
	if (img >= IMG_FOLDER_UP)
		return;
#endif

	col++;

3372
	/* ouput file name */
3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383
	if (calcWidthCol == -1)
		output_text(pane, dis, col, entry->data.cFileName, 0);
	else if (calcWidthCol==col || calcWidthCol==COLUMNS)
		calc_width(pane, dis, col, entry->data.cFileName);

	col++;

#ifdef _NO_EXTENSIONS
  if (!pane->treePane) {
#endif

3384
        /* display file size */
3385 3386 3387 3388 3389
	if (visible_cols & COL_SIZE) {
#ifdef _NO_EXTENSIONS
		if (!(attrs&FILE_ATTRIBUTE_DIRECTORY))
#endif
		{
3390
			ULONGLONG size;
3391

3392
                        size = ((ULONGLONG)entry->data.nFileSizeHigh << 32) | entry->data.nFileSizeLow;
3393

3394
			_stprintf(buffer, sLongNumFmt, size);
3395 3396 3397 3398

			if (calcWidthCol == -1)
				output_number(pane, dis, col, buffer);
			else if (calcWidthCol==col || calcWidthCol==COLUMNS)
3399
				calc_width(pane, dis, col, buffer);/*TODO: not ever time enough */
3400 3401 3402 3403 3404
		}

		col++;
	}

3405
	/* display file date */
3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420
	if (visible_cols & (COL_DATE|COL_TIME)) {
#ifndef _NO_EXTENSIONS
		format_date(&entry->data.ftCreationTime, buffer, visible_cols);
		if (calcWidthCol == -1)
			output_text(pane, dis, col, buffer, 0);
		else if (calcWidthCol==col || calcWidthCol==COLUMNS)
			calc_width(pane, dis, col, buffer);
		col++;

		format_date(&entry->data.ftLastAccessTime, buffer, visible_cols);
		if (calcWidthCol == -1)
			output_text(pane, dis, col, buffer, 0);
		else if (calcWidthCol==col || calcWidthCol==COLUMNS)
			calc_width(pane, dis, col, buffer);
		col++;
3421
#endif /* _NO_EXTENSIONS */
3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432

		format_date(&entry->data.ftLastWriteTime, buffer, visible_cols);
		if (calcWidthCol == -1)
			output_text(pane, dis, col, buffer, 0);
		else if (calcWidthCol==col || calcWidthCol==COLUMNS)
			calc_width(pane, dis, col, buffer);
		col++;
	}

#ifndef _NO_EXTENSIONS
	if (entry->bhfi_valid) {
3433
            ULONGLONG index = ((ULONGLONG)entry->bhfi.nFileIndexHigh << 32) | entry->bhfi.nFileIndexLow;
3434 3435

		if (visible_cols & COL_INDEX) {
3436 3437
			_stprintf(buffer, sLongHexFmt, index);

3438 3439 3440 3441
			if (calcWidthCol == -1)
				output_text(pane, dis, col, buffer, DT_RIGHT);
			else if (calcWidthCol==col || calcWidthCol==COLUMNS)
				calc_width(pane, dis, col, buffer);
3442

3443 3444 3445 3446
			col++;
		}

		if (visible_cols & COL_LINKS) {
3447
			wsprintf(buffer, sNumFmt, entry->bhfi.nNumberOfLinks);
3448

3449 3450 3451 3452
			if (calcWidthCol == -1)
				output_text(pane, dis, col, buffer, DT_CENTER);
			else if (calcWidthCol==col || calcWidthCol==COLUMNS)
				calc_width(pane, dis, col, buffer);
3453

3454 3455 3456 3457
			col++;
		}
	} else
		col += 2;
3458
#endif /* _NO_EXTENSIONS */
3459

3460
	/* show file attributes */
3461 3462
	if (visible_cols & COL_ATTRIBUTES) {
#ifdef _NO_EXTENSIONS
3463
		static const TCHAR s4Tabs[] = {' ','\t',' ','\t',' ','\t',' ','\t',' ','\0'};
3464
		lstrcpy(buffer, s4Tabs);
3465
#else
3466
		static const TCHAR s11Tabs[] = {' ','\t',' ','\t',' ','\t',' ','\t',' ','\t',' ','\t',' ','\t',' ','\t',' ','\t',' ','\t',' ','\t',' ','\0'};
3467
		lstrcpy(buffer, s11Tabs);
3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484
#endif

		if (attrs & FILE_ATTRIBUTE_NORMAL)					buffer[ 0] = 'N';
		else {
			if (attrs & FILE_ATTRIBUTE_READONLY)			buffer[ 2] = 'R';
			if (attrs & FILE_ATTRIBUTE_HIDDEN)				buffer[ 4] = 'H';
			if (attrs & FILE_ATTRIBUTE_SYSTEM)				buffer[ 6] = 'S';
			if (attrs & FILE_ATTRIBUTE_ARCHIVE)				buffer[ 8] = 'A';
			if (attrs & FILE_ATTRIBUTE_COMPRESSED)			buffer[10] = 'C';
#ifndef _NO_EXTENSIONS
			if (attrs & FILE_ATTRIBUTE_DIRECTORY)			buffer[12] = 'D';
			if (attrs & FILE_ATTRIBUTE_ENCRYPTED)			buffer[14] = 'E';
			if (attrs & FILE_ATTRIBUTE_TEMPORARY)			buffer[16] = 'T';
			if (attrs & FILE_ATTRIBUTE_SPARSE_FILE)			buffer[18] = 'P';
			if (attrs & FILE_ATTRIBUTE_REPARSE_POINT)		buffer[20] = 'Q';
			if (attrs & FILE_ATTRIBUTE_OFFLINE)				buffer[22] = 'O';
			if (attrs & FILE_ATTRIBUTE_NOT_CONTENT_INDEXED)	buffer[24] = 'X';
3485
#endif /* _NO_EXTENSIONS */
3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497
		}

		if (calcWidthCol == -1)
			output_tabbed_text(pane, dis, col, buffer);
		else if (calcWidthCol==col || calcWidthCol==COLUMNS)
			calc_tabbed_width(pane, dis, col, buffer);

		col++;
	}

/*TODO
	if (flags.security) {
3498
		static const TCHAR sSecTabs[] = {
3499 3500 3501 3502 3503 3504 3505 3506
			' ','\t',' ','\t',' ','\t',' ',
			' ','\t',' ',
			' ','\t',' ','\t',' ','\t',' ',
			' ','\t',' ',
			' ','\t',' ','\t',' ','\t',' ',
			'\0'
		};

3507 3508
		DWORD rights = get_access_mask();

3509
		lstrcpy(buffer, sSecTabs);
3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535

		if (rights & FILE_READ_DATA)			buffer[ 0] = 'R';
		if (rights & FILE_WRITE_DATA)			buffer[ 2] = 'W';
		if (rights & FILE_APPEND_DATA)			buffer[ 4] = 'A';
		if (rights & FILE_READ_EA)				{buffer[6] = 'entry'; buffer[ 7] = 'R';}
		if (rights & FILE_WRITE_EA)				{buffer[9] = 'entry'; buffer[10] = 'W';}
		if (rights & FILE_EXECUTE)				buffer[12] = 'X';
		if (rights & FILE_DELETE_CHILD)			buffer[14] = 'D';
		if (rights & FILE_READ_ATTRIBUTES)		{buffer[16] = 'a'; buffer[17] = 'R';}
		if (rights & FILE_WRITE_ATTRIBUTES)		{buffer[19] = 'a'; buffer[20] = 'W';}
		if (rights & WRITE_DAC)					buffer[22] = 'C';
		if (rights & WRITE_OWNER)				buffer[24] = 'O';
		if (rights & SYNCHRONIZE)				buffer[26] = 'S';

		output_text(dis, col++, buffer, DT_LEFT, 3, psize);
	}

	if (flags.description) {
		get_description(buffer);
		output_text(dis, col++, buffer, 0, psize);
	}
*/

#ifdef _NO_EXTENSIONS
  }

3536
        /* draw focus frame */
3537
	if ((dis->itemState&ODS_FOCUS) && calcWidthCol==-1) {
3538 3539
	        /* Currently [04/2000] Wine neither behaves exactly the same */
	        /* way as WIN 95 nor like Windows NT... */
3540 3541 3542 3543
		HGDIOBJ lastBrush;
		HPEN lastPen;
		HPEN hpen;

3544
		if (!(GetVersion() & 0x80000000)) {	/* Windows NT? */
3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557
			LOGBRUSH lb = {PS_SOLID, RGB(255,255,255)};
			hpen = ExtCreatePen(PS_COSMETIC|PS_ALTERNATE, 1, &lb, 0, 0);
		} else
			hpen = CreatePen(PS_DOT, 0, RGB(255,255,255));

		lastPen = SelectPen(dis->hDC, hpen);
		lastBrush = SelectObject(dis->hDC, GetStockObject(HOLLOW_BRUSH));
		SetROP2(dis->hDC, R2_XORPEN);
		Rectangle(dis->hDC, focusRect.left, focusRect.top, focusRect.right, focusRect.bottom);
		SelectObject(dis->hDC, lastBrush);
		SelectObject(dis->hDC, lastPen);
		DeleteObject(hpen);
	}
3558
#endif /* _NO_EXTENSIONS */
3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578
}


#ifdef _NO_EXTENSIONS

static void draw_splitbar(HWND hwnd, int x)
{
	RECT rt;
	HDC hdc = GetDC(hwnd);

	GetClientRect(hwnd, &rt);

	rt.left = x - SPLIT_WIDTH/2;
	rt.right = x + SPLIT_WIDTH/2+1;

	InvertRect(hdc, &rt);

	ReleaseDC(hwnd, hdc);
}

3579
#endif /* _NO_EXTENSIONS */
3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594


#ifndef _NO_EXTENSIONS

static void set_header(Pane* pane)
{
	HD_ITEM item;
	int scroll_pos = GetScrollPos(pane->hwnd, SB_HORZ);
	int i=0, x=0;

	item.mask = HDI_WIDTH;
	item.cxy = 0;

	for(; x+pane->widths[i]<scroll_pos && i<COLUMNS; i++) {
		x += pane->widths[i];
3595
		SendMessage(pane->hwndHeader, HDM_SETITEM, i, (LPARAM) &item);
3596 3597 3598 3599 3600
	}

	if (i < COLUMNS) {
		x += pane->widths[i];
		item.cxy = x - scroll_pos;
3601
		SendMessage(pane->hwndHeader, HDM_SETITEM, i++, (LPARAM) &item);
3602 3603 3604 3605

		for(; i<COLUMNS; i++) {
			item.cxy = pane->widths[i];
			x += pane->widths[i];
3606
			SendMessage(pane->hwndHeader, HDM_SETITEM, i, (LPARAM) &item);
3607 3608 3609 3610 3611 3612 3613
		}
	}
}

static LRESULT pane_notify(Pane* pane, NMHDR* pnmh)
{
	switch(pnmh->code) {
3614
		case HDN_ITEMCHANGED: {
3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629
			HD_NOTIFY* phdn = (HD_NOTIFY*) pnmh;
			int idx = phdn->iItem;
			int dx = phdn->pitem->cxy - pane->widths[idx];
			int i;

			RECT clnt;
			GetClientRect(pane->hwnd, &clnt);

			pane->widths[idx] += dx;

			for(i=idx; ++i<=COLUMNS; )
				pane->positions[i] += dx;

			{
				int scroll_pos = GetScrollPos(pane->hwnd, SB_HORZ);
3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641
				RECT rt_scr;
				RECT rt_clip;

				rt_scr.left   = pane->positions[idx+1]-scroll_pos;
				rt_scr.top    = 0;
				rt_scr.right  = clnt.right;
				rt_scr.bottom = clnt.bottom;

				rt_clip.left   = pane->positions[idx]-scroll_pos;
				rt_clip.top    = 0;
				rt_clip.right  = clnt.right;
				rt_clip.bottom = clnt.bottom;
3642 3643 3644 3645 3646 3647 3648 3649 3650 3651

				if (rt_scr.left < 0) rt_scr.left = 0;
				if (rt_clip.left < 0) rt_clip.left = 0;

				ScrollWindowEx(pane->hwnd, dx, 0, &rt_scr, &rt_clip, 0, 0, SW_INVALIDATE);

				rt_clip.right = pane->positions[idx+1];
				RedrawWindow(pane->hwnd, &rt_clip, 0, RDW_INVALIDATE|RDW_UPDATENOW);

				if (pnmh->code == HDN_ENDTRACK) {
3652
					SendMessage(pane->hwnd, LB_SETHORIZONTALEXTENT, pane->positions[COLUMNS], 0);
3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669

					if (GetScrollPos(pane->hwnd, SB_HORZ) != scroll_pos)
						set_header(pane);
				}
			}

			return FALSE;
		}

		case HDN_DIVIDERDBLCLICK: {
			HD_NOTIFY* phdn = (HD_NOTIFY*) pnmh;
			HD_ITEM item;

			calc_single_width(pane, phdn->iItem);
			item.mask = HDI_WIDTH;
			item.cxy = pane->widths[phdn->iItem];

3670
			SendMessage(pane->hwndHeader, HDM_SETITEM, phdn->iItem, (LPARAM) &item);
3671 3672 3673 3674 3675 3676 3677
			InvalidateRect(pane->hwnd, 0, TRUE);
			break;}
	}

	return 0;
}

3678
#endif /* _NO_EXTENSIONS */
3679 3680


3681
static void scan_entry(ChildWnd* child, Entry* entry, int idx, HWND hwnd)
3682 3683
{
	TCHAR path[MAX_PATH];
3684
	HCURSOR old_cursor = SetCursor(LoadCursor(0, IDC_WAIT));
3685

3686
	/* delete sub entries in left pane */
3687
	for(;;) {
3688
		LRESULT res = SendMessage(child->left.hwnd, LB_GETITEMDATA, idx+1, 0);
3689 3690 3691 3692 3693
		Entry* sub = (Entry*) res;

		if (res==LB_ERR || !sub || sub->level<=entry->level)
			break;

3694
		SendMessage(child->left.hwnd, LB_DELETESTRING, idx+1, 0);
3695 3696
	}

3697
	/* empty right pane */
3698
	SendMessage(child->right.hwnd, LB_RESETCONTENT, 0, 0);
3699

3700
	/* release memory */
3701 3702
	free_entries(entry);

3703
	/* read contents from disk */
3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714
#ifdef _SHELL_FOLDERS
	if (entry->etype == ET_SHELL)
	{
		read_directory(entry, NULL, child->sortOrder, hwnd);
	}
	else
#endif
	{
		get_path(entry, path);
		read_directory(entry, path, child->sortOrder, hwnd);
	}
3715

3716
	/* insert found entries in right pane */
3717
	insert_entries(&child->right, entry->down, child->filter_pattern, child->filter_flags, -1);
3718 3719 3720 3721 3722 3723 3724
	calc_widths(&child->right, FALSE);
#ifndef _NO_EXTENSIONS
	set_header(&child->right);
#endif

	child->header_wdths_ok = FALSE;

3725
	SetCursor(old_cursor);
3726 3727 3728
}


3729
/* expand a directory entry */
3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748

static BOOL expand_entry(ChildWnd* child, Entry* dir)
{
	int idx;
	Entry* p;

	if (!dir || dir->expanded || !dir->down)
		return FALSE;

	p = dir->down;

	if (p->data.cFileName[0]=='.' && p->data.cFileName[1]=='\0' && p->next) {
		p = p->next;

		if (p->data.cFileName[0]=='.' && p->data.cFileName[1]=='.' &&
				p->data.cFileName[2]=='\0' && p->next)
			p = p->next;
	}

3749
	/* no subdirectories ? */
3750 3751 3752
	if (!(p->data.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY))
		return FALSE;

3753
	idx = SendMessage(child->left.hwnd, LB_FINDSTRING, 0, (LPARAM)dir);
3754 3755 3756

	dir->expanded = TRUE;

3757
	/* insert entries in left pane */
3758
	insert_entries(&child->left, p, NULL, TF_ALL, idx);
3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775

	if (!child->header_wdths_ok) {
		if (calc_widths(&child->left, FALSE)) {
#ifndef _NO_EXTENSIONS
			set_header(&child->left);
#endif

			child->header_wdths_ok = TRUE;
		}
	}

	return TRUE;
}


static void collapse_entry(Pane* pane, Entry* dir)
{
3776
	int idx = SendMessage(pane->hwnd, LB_FINDSTRING, 0, (LPARAM)dir);
3777 3778 3779

	ShowWindow(pane->hwnd, SW_HIDE);

3780
	/* hide sub entries */
3781
	for(;;) {
3782
		LRESULT res = SendMessage(pane->hwnd, LB_GETITEMDATA, idx+1, 0);
3783 3784 3785 3786 3787
		Entry* sub = (Entry*) res;

		if (res==LB_ERR || !sub || sub->level<=dir->level)
			break;

3788
		SendMessage(pane->hwnd, LB_DELETESTRING, idx+1, 0);
3789 3790 3791 3792 3793 3794 3795 3796
	}

	dir->expanded = FALSE;

	ShowWindow(pane->hwnd, SW_SHOW);
}


3797 3798
static void refresh_right_pane(ChildWnd* child)
{
3799
	SendMessage(child->right.hwnd, LB_RESETCONTENT, 0, 0);
3800 3801 3802 3803 3804 3805 3806 3807
	insert_entries(&child->right, child->right.root, child->filter_pattern, child->filter_flags, -1);
	calc_widths(&child->right, FALSE);

#ifndef _NO_EXTENSIONS
	set_header(&child->right);
#endif
}

3808
static void set_curdir(ChildWnd* child, Entry* entry, int idx, HWND hwnd)
3809 3810 3811
{
	TCHAR path[MAX_PATH];

3812 3813 3814
	if (!entry)
		return;

3815 3816
	path[0] = '\0';

3817
	child->left.cur = entry;
3818

3819
	child->right.root = entry->down? entry->down: entry;
3820 3821 3822
	child->right.cur = entry;

	if (!entry->scanned)
3823
		scan_entry(child, entry, idx, hwnd);
3824 3825
	else
		refresh_right_pane(child);
3826 3827 3828

	get_path(entry, path);
	lstrcpy(child->path, path);
3829 3830 3831 3832

	if (child->hwnd)	/* only change window title, if the window already exists */
		SetWindowText(child->hwnd, path);

3833
	if (path[0])
3834 3835
		if (SetCurrentDirectory(path))
			set_space_status();
3836 3837 3838
}


3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851
static void refresh_child(ChildWnd* child)
{
	TCHAR path[MAX_PATH], drv[_MAX_DRIVE+1];
	Entry* entry;
	int idx;

	get_path(child->left.cur, path);
	_tsplitpath(path, drv, NULL, NULL, NULL);

	child->right.root = NULL;

	scan_entry(child, &child->root.entry, 0, child->hwnd);

3852 3853 3854 3855 3856 3857 3858 3859 3860
#ifdef _SHELL_FOLDERS
	if (child->root.entry.etype == ET_SHELL)
		entry = read_tree(&child->root, NULL, get_path_pidl(path,child->hwnd), drv, child->sortOrder, child->hwnd);
	else
#endif
		entry = read_tree(&child->root, path, NULL, drv, child->sortOrder, child->hwnd);

	if (!entry)
		entry = &child->root.entry;
3861

3862
	insert_entries(&child->left, child->root.entry.down, NULL, TF_ALL, 0);
3863 3864 3865

	set_curdir(child, entry, 0, child->hwnd);

3866 3867
	idx = SendMessage(child->left.hwnd, LB_FINDSTRING, 0, (LPARAM)child->left.cur);
	SendMessage(child->left.hwnd, LB_SETCURSEL, idx, 0);
3868 3869 3870
}


Mike McCormack's avatar
Mike McCormack committed
3871
static void create_drive_bar(void)
3872 3873
{
	TBBUTTON drivebarBtn = {0, 0, TBSTATE_ENABLED, BTNS_BUTTON, {0, 0}, 0, 0};
3874
#ifndef _NO_EXTENSIONS
3875
	TCHAR b1[BUFFER_LEN];
3876
#endif
3877 3878 3879 3880 3881 3882 3883
	int btn = 1;
	PTSTR p;

	GetLogicalDriveStrings(BUFFER_LEN, Globals.drives);

	Globals.hdrivebar = CreateToolbarEx(Globals.hMainWnd, WS_CHILD|WS_VISIBLE|CCS_NOMOVEY|TBSTYLE_LIST,
				IDW_DRIVEBAR, 2, Globals.hInstance, IDB_DRIVEBAR, &drivebarBtn,
3884
				0, 16, 13, 16, 13, sizeof(TBBUTTON));
3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936

#ifndef _NO_EXTENSIONS
#ifdef __WINE__
	/* insert unix file system button */
	b1[0] = '/';
	b1[1] = '\0';
	b1[2] = '\0';
	SendMessage(Globals.hdrivebar, TB_ADDSTRING, 0, (LPARAM)b1);

	drivebarBtn.idCommand = ID_DRIVE_UNIX_FS;
	SendMessage(Globals.hdrivebar, TB_INSERTBUTTON, btn++, (LPARAM)&drivebarBtn);
	drivebarBtn.iString++;
#endif
#ifdef _SHELL_FOLDERS
	/* insert shell namespace button */
	load_string(b1, IDS_SHELL);
	b1[lstrlen(b1)+1] = '\0';
	SendMessage(Globals.hdrivebar, TB_ADDSTRING, 0, (LPARAM)b1);

	drivebarBtn.idCommand = ID_DRIVE_SHELL_NS;
	SendMessage(Globals.hdrivebar, TB_INSERTBUTTON, btn++, (LPARAM)&drivebarBtn);
	drivebarBtn.iString++;
#endif

	/* register windows drive root strings */
	SendMessage(Globals.hdrivebar, TB_ADDSTRING, 0, (LPARAM)Globals.drives);
#endif

	drivebarBtn.idCommand = ID_DRIVE_FIRST;

	for(p=Globals.drives; *p; ) {
#ifdef _NO_EXTENSIONS
		/* insert drive letter */
		TCHAR b[3] = {tolower(*p)};
		SendMessage(Globals.hdrivebar, TB_ADDSTRING, 0, (LPARAM)b);
#endif
		switch(GetDriveType(p)) {
			case DRIVE_REMOVABLE:	drivebarBtn.iBitmap = 1;	break;
			case DRIVE_CDROM:		drivebarBtn.iBitmap = 3;	break;
			case DRIVE_REMOTE:		drivebarBtn.iBitmap = 4;	break;
			case DRIVE_RAMDISK:		drivebarBtn.iBitmap = 5;	break;
			default:/*DRIVE_FIXED*/	drivebarBtn.iBitmap = 2;
		}

		SendMessage(Globals.hdrivebar, TB_INSERTBUTTON, btn++, (LPARAM)&drivebarBtn);
		drivebarBtn.idCommand++;
		drivebarBtn.iString++;

		while(*p++);
	}
}

Mike McCormack's avatar
Mike McCormack committed
3937
static void refresh_drives(void)
3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953
{
	RECT rect;

	/* destroy drive bar */
	DestroyWindow(Globals.hdrivebar);
	Globals.hdrivebar = 0;

	/* re-create drive bar */
	create_drive_bar();

	/* update window layout */
	GetClientRect(Globals.hMainWnd, &rect);
	SendMessage(Globals.hMainWnd, WM_SIZE, 0, MAKELONG(rect.right, rect.bottom));
}


Mike McCormack's avatar
Mike McCormack committed
3954
static BOOL launch_file(HWND hwnd, LPCTSTR cmd, UINT nCmdShow)
3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966
{
	HINSTANCE hinst = ShellExecute(hwnd, NULL/*operation*/, cmd, NULL/*parameters*/, NULL/*dir*/, nCmdShow);

	if ((int)hinst <= 32) {
		display_error(hwnd, GetLastError());
		return FALSE;
	}

	return TRUE;
}


Mike McCormack's avatar
Mike McCormack committed
3967
static BOOL launch_entry(Entry* entry, HWND hwnd, UINT nCmdShow)
3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979
{
	TCHAR cmd[MAX_PATH];

#ifdef _SHELL_FOLDERS
	if (entry->etype == ET_SHELL) {
		BOOL ret = TRUE;

		SHELLEXECUTEINFO shexinfo;

		shexinfo.cbSize = sizeof(SHELLEXECUTEINFO);
		shexinfo.fMask = SEE_MASK_IDLIST;
		shexinfo.hwnd = hwnd;
3980 3981 3982 3983
		shexinfo.lpVerb = NULL;
		shexinfo.lpFile = NULL;
		shexinfo.lpParameters = NULL;
		shexinfo.lpDirectory = NULL;
3984 3985 3986 3987 3988 3989 3990 3991 3992
		shexinfo.nShow = nCmdShow;
		shexinfo.lpIDList = get_to_absolute_pidl(entry, hwnd);

		if (!ShellExecuteEx(&shexinfo)) {
			display_error(hwnd, GetLastError());
			ret = FALSE;
		}

		if (shexinfo.lpIDList != entry->pidl)
3993
			IMalloc_Free(Globals.iMalloc, shexinfo.lpIDList);
3994 3995 3996 3997 3998 3999 4000 4001 4002

		return ret;
	}
#endif

	get_path(entry, cmd);

	 /* start program, open document... */
	return launch_file(hwnd, cmd, nCmdShow);
4003 4004 4005
}


4006
static void activate_entry(ChildWnd* child, Pane* pane, HWND hwnd)
4007 4008 4009 4010 4011 4012 4013 4014 4015 4016
{
	Entry* entry = pane->cur;

	if (!entry)
		return;

	if (entry->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
		int scanned_old = entry->scanned;

		if (!scanned_old)
4017 4018 4019 4020
		{
			int idx = SendMessage(child->left.hwnd, LB_GETCURSEL, 0, 0);
			scan_entry(child, entry, idx, hwnd);
		}
4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036

#ifndef _NO_EXTENSIONS
		if (entry->data.cFileName[0]=='.' && entry->data.cFileName[1]=='\0')
			return;
#endif

		if (entry->data.cFileName[0]=='.' && entry->data.cFileName[1]=='.' && entry->data.cFileName[2]=='\0') {
			entry = child->left.cur->up;
			collapse_entry(&child->left, entry);
			goto focus_entry;
		} else if (entry->expanded)
			collapse_entry(pane, child->left.cur);
		else {
			expand_entry(child, child->left.cur);

			if (!pane->treePane) focus_entry: {
4037 4038 4039
				int idxstart = SendMessage(child->left.hwnd, LB_GETCURSEL, 0, 0);
				int idx = SendMessage(child->left.hwnd, LB_FINDSTRING, idxstart, (LPARAM)entry);
				SendMessage(child->left.hwnd, LB_SETCURSEL, idx, 0);
4040
				set_curdir(child, entry, idx, hwnd);
4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051
			}
		}

		if (!scanned_old) {
			calc_widths(pane, FALSE);

#ifndef _NO_EXTENSIONS
			set_header(pane);
#endif
		}
	} else {
4052 4053 4054 4055
		if (GetKeyState(VK_MENU) < 0)
			show_properties_dlg(entry, child->hwnd);
		else
			launch_entry(entry, child->hwnd, SW_SHOWNORMAL);
4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091
	}
}


static BOOL pane_command(Pane* pane, UINT cmd)
{
	switch(cmd) {
		case ID_VIEW_NAME:
			if (pane->visible_cols) {
				pane->visible_cols = 0;
				calc_widths(pane, TRUE);
#ifndef _NO_EXTENSIONS
				set_header(pane);
#endif
				InvalidateRect(pane->hwnd, 0, TRUE);
				CheckMenuItem(Globals.hMenuView, ID_VIEW_NAME, MF_BYCOMMAND|MF_CHECKED);
				CheckMenuItem(Globals.hMenuView, ID_VIEW_ALL_ATTRIBUTES, MF_BYCOMMAND);
				CheckMenuItem(Globals.hMenuView, ID_VIEW_SELECTED_ATTRIBUTES, MF_BYCOMMAND);
			}
			break;

		case ID_VIEW_ALL_ATTRIBUTES:
			if (pane->visible_cols != COL_ALL) {
				pane->visible_cols = COL_ALL;
				calc_widths(pane, TRUE);
#ifndef _NO_EXTENSIONS
				set_header(pane);
#endif
				InvalidateRect(pane->hwnd, 0, TRUE);
				CheckMenuItem(Globals.hMenuView, ID_VIEW_NAME, MF_BYCOMMAND);
				CheckMenuItem(Globals.hMenuView, ID_VIEW_ALL_ATTRIBUTES, MF_BYCOMMAND|MF_CHECKED);
				CheckMenuItem(Globals.hMenuView, ID_VIEW_SELECTED_ATTRIBUTES, MF_BYCOMMAND);
			}
			break;

#ifndef _NO_EXTENSIONS
4092
		case ID_PREFERRED_SIZES: {
4093 4094 4095 4096 4097 4098
			calc_widths(pane, TRUE);
			set_header(pane);
			InvalidateRect(pane->hwnd, 0, TRUE);
			break;}
#endif

4099
		        /* TODO: more command ids... */
4100 4101 4102 4103 4104 4105 4106 4107 4108

		default:
			return FALSE;
	}

	return TRUE;
}


4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125
static void set_sort_order(ChildWnd* child, SORT_ORDER sortOrder)
{
	if (child->sortOrder != sortOrder) {
		child->sortOrder = sortOrder;
		refresh_child(child);
	}
}

static void update_view_menu(ChildWnd* child)
{
	CheckMenuItem(Globals.hMenuView, ID_VIEW_SORT_NAME, child->sortOrder==SORT_NAME? MF_CHECKED: MF_UNCHECKED);
	CheckMenuItem(Globals.hMenuView, ID_VIEW_SORT_TYPE, child->sortOrder==SORT_EXT? MF_CHECKED: MF_UNCHECKED);
	CheckMenuItem(Globals.hMenuView, ID_VIEW_SORT_SIZE, child->sortOrder==SORT_SIZE? MF_CHECKED: MF_UNCHECKED);
	CheckMenuItem(Globals.hMenuView, ID_VIEW_SORT_DATE, child->sortOrder==SORT_DATE? MF_CHECKED: MF_UNCHECKED);
}


4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163
static BOOL is_directory(LPCTSTR target)
{
	/*TODO correctly handle UNIX paths */
	DWORD target_attr = GetFileAttributes(target);

	if (target_attr == INVALID_FILE_ATTRIBUTES)
		return FALSE;

	return target_attr&FILE_ATTRIBUTE_DIRECTORY? TRUE: FALSE;
}
	
static BOOL prompt_target(Pane* pane, LPTSTR source, LPTSTR target)
{
	TCHAR path[MAX_PATH];
	int len;

	get_path(pane->cur, path);

	if (DialogBoxParam(Globals.hInstance, MAKEINTRESOURCE(IDD_SELECT_DESTINATION), pane->hwnd, DestinationDlgProc, (LPARAM)path) != IDOK)
		return FALSE;

	get_path(pane->cur, source);

	/* convert relative targets to absolute paths */
	if (path[0]!='/' && path[1]!=':') {
		get_path(pane->cur->up, target);
		len = lstrlen(target);

		if (target[len-1]!='\\' && target[len-1]!='/')
			target[len++] = '/';

		lstrcpy(target+len, path);
	} else
		lstrcpy(target, path);

	/* If the target already exists as directory, create a new target below this. */
	if (is_directory(path)) {
		TCHAR fname[_MAX_FNAME], ext[_MAX_EXT];
4164
		static const TCHAR sAppend[] = {'%','s','/','%','s','%','s','\0'};
4165 4166 4167 4168 4169 4170 4171 4172 4173 4174

		_tsplitpath(source, NULL, NULL, fname, ext);

		wsprintf(target, sAppend, path, fname, ext);
	}

	return TRUE;
}


4175 4176 4177
static IContextMenu2* s_pctxmenu2 = NULL;
static IContextMenu3* s_pctxmenu3 = NULL;

4178
static void CtxMenu_reset(void)
4179 4180 4181 4182 4183
{
	s_pctxmenu2 = NULL;
	s_pctxmenu3 = NULL;
}

Mike McCormack's avatar
Mike McCormack committed
4184
static IContextMenu* CtxMenu_query_interfaces(IContextMenu* pcm1)
4185 4186 4187 4188 4189
{
	IContextMenu* pcm = NULL;

	CtxMenu_reset();

4190
	if (IContextMenu_QueryInterface(pcm1, &IID_IContextMenu3, (void**)&pcm) == NOERROR)
4191
		s_pctxmenu3 = (LPCONTEXTMENU3)pcm;
4192
	else if (IContextMenu_QueryInterface(pcm1, &IID_IContextMenu2, (void**)&pcm) == NOERROR)
4193 4194 4195
		s_pctxmenu2 = (LPCONTEXTMENU2)pcm;

	if (pcm) {
4196
		IContextMenu_Release(pcm1);
4197 4198 4199 4200 4201 4202 4203 4204
		return pcm;
	} else
		return pcm1;
}

static BOOL CtxMenu_HandleMenuMsg(UINT nmsg, WPARAM wparam, LPARAM lparam)
{
	if (s_pctxmenu3) {
4205
		if (SUCCEEDED(IContextMenu3_HandleMenuMsg(s_pctxmenu3, nmsg, wparam, lparam)))
4206 4207 4208 4209
			return TRUE;
	}

	if (s_pctxmenu2)
4210
		if (SUCCEEDED(IContextMenu2_HandleMenuMsg(s_pctxmenu2, nmsg, wparam, lparam)))
4211 4212 4213 4214 4215 4216
			return TRUE;

	return FALSE;
}


4217 4218 4219
static HRESULT ShellFolderContextMenu(IShellFolder* shell_folder, HWND hwndParent, int cidl, LPCITEMIDLIST* apidl, int x, int y)
{
	IContextMenu* pcm;
4220
	BOOL executed = FALSE;
4221

4222
	HRESULT hr = IShellFolder_GetUIObjectOf(shell_folder, hwndParent, cidl, apidl, &IID_IContextMenu, NULL, (LPVOID*)&pcm);
4223 4224 4225 4226 4227
/*	HRESULT hr = CDefFolderMenu_Create2(dir?dir->_pidl:DesktopFolder(), hwndParent, 1, &pidl, shell_folder, NULL, 0, NULL, &pcm); */

	if (SUCCEEDED(hr)) {
		HMENU hmenu = CreatePopupMenu();

4228 4229
		pcm = CtxMenu_query_interfaces(pcm);

4230
		if (hmenu) {
4231
			hr = IContextMenu_QueryContextMenu(pcm, hmenu, 0, FCIDM_SHVIEWFIRST, FCIDM_SHVIEWLAST, CMF_NORMAL);
4232 4233 4234 4235

			if (SUCCEEDED(hr)) {
				UINT idCmd = TrackPopupMenu(hmenu, TPM_LEFTALIGN|TPM_RETURNCMD|TPM_RIGHTBUTTON, x, y, 0, hwndParent, NULL);

4236 4237
				CtxMenu_reset();

4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250
				if (idCmd) {
				  CMINVOKECOMMANDINFO cmi;

				  cmi.cbSize = sizeof(CMINVOKECOMMANDINFO);
				  cmi.fMask = 0;
				  cmi.hwnd = hwndParent;
				  cmi.lpVerb = (LPCSTR)(INT_PTR)(idCmd - FCIDM_SHVIEWFIRST);
				  cmi.lpParameters = NULL;
				  cmi.lpDirectory = NULL;
				  cmi.nShow = SW_SHOWNORMAL;
				  cmi.dwHotKey = 0;
				  cmi.hIcon = 0;

4251
				  hr = IContextMenu_InvokeCommand(pcm, &cmi);
4252
					executed = TRUE;
4253
				}
4254 4255
			} else
				CtxMenu_reset();
4256 4257
		}

4258
		IContextMenu_Release(pcm);
4259 4260
	}

4261
	return FAILED(hr)? hr: executed? S_OK: S_FALSE;
4262 4263 4264
}


Mike McCormack's avatar
Mike McCormack committed
4265
static LRESULT CALLBACK ChildWndProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam)
4266
{
4267
	ChildWnd* child = (ChildWnd*) GetWindowLongPtr(hwnd, GWLP_USERDATA);
4268 4269 4270 4271 4272 4273 4274 4275 4276
	ASSERT(child);

	switch(nmsg) {
		case WM_DRAWITEM: {
			LPDRAWITEMSTRUCT dis = (LPDRAWITEMSTRUCT)lparam;
			Entry* entry = (Entry*) dis->itemData;

			if (dis->CtlID == IDW_TREE_LEFT)
				draw_item(&child->left, dis, entry, -1);
4277
			else if (dis->CtlID == IDW_TREE_RIGHT)
4278
				draw_item(&child->right, dis, entry, -1);
4279 4280
			else
				goto draw_menu_item;
4281 4282 4283 4284 4285 4286 4287 4288 4289

			return TRUE;}

		case WM_CREATE:
			InitChildWindow(child);
			break;

		case WM_NCDESTROY:
			free_child_window(child);
4290
			SetWindowLongPtr(hwnd, GWLP_USERDATA, 0);
4291 4292 4293 4294 4295 4296 4297 4298 4299 4300
			break;

		case WM_PAINT: {
			PAINTSTRUCT ps;
			HBRUSH lastBrush;
			RECT rt;
			GetClientRect(hwnd, &rt);
			BeginPaint(hwnd, &ps);
			rt.left = child->split_pos-SPLIT_WIDTH/2;
			rt.right = child->split_pos+SPLIT_WIDTH/2+1;
4301
			lastBrush = SelectObject(ps.hdc, GetStockObject(COLOR_SPLITBAR));
4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325
			Rectangle(ps.hdc, rt.left, rt.top-1, rt.right, rt.bottom+1);
			SelectObject(ps.hdc, lastBrush);
#ifdef _NO_EXTENSIONS
			rt.top = rt.bottom - GetSystemMetrics(SM_CYHSCROLL);
			FillRect(ps.hdc, &rt, GetStockObject(BLACK_BRUSH));
#endif
			EndPaint(hwnd, &ps);
			break;}

		case WM_SETCURSOR:
			if (LOWORD(lparam) == HTCLIENT) {
				POINT pt;
				GetCursorPos(&pt);
				ScreenToClient(hwnd, &pt);

				if (pt.x>=child->split_pos-SPLIT_WIDTH/2 && pt.x<child->split_pos+SPLIT_WIDTH/2+1) {
					SetCursor(LoadCursor(0, IDC_SIZEWE));
					return TRUE;
				}
			}
			goto def;

		case WM_LBUTTONDOWN: {
			RECT rt;
4326
                        int x = (short)LOWORD(lparam);
4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343

			GetClientRect(hwnd, &rt);

			if (x>=child->split_pos-SPLIT_WIDTH/2 && x<child->split_pos+SPLIT_WIDTH/2+1) {
				last_split = child->split_pos;
#ifdef _NO_EXTENSIONS
				draw_splitbar(hwnd, last_split);
#endif
				SetCapture(hwnd);
			}

			break;}

		case WM_LBUTTONUP:
			if (GetCapture() == hwnd) {
#ifdef _NO_EXTENSIONS
				RECT rt;
4344
                                int x = (short)LOWORD(lparam);
4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381
				draw_splitbar(hwnd, last_split);
				last_split = -1;
				GetClientRect(hwnd, &rt);
				child->split_pos = x;
				resize_tree(child, rt.right, rt.bottom);
#endif
				ReleaseCapture();
			}
			break;

#ifdef _NO_EXTENSIONS
		case WM_CAPTURECHANGED:
			if (GetCapture()==hwnd && last_split>=0)
				draw_splitbar(hwnd, last_split);
			break;
#endif

		case WM_KEYDOWN:
			if (wparam == VK_ESCAPE)
				if (GetCapture() == hwnd) {
					RECT rt;
#ifdef _NO_EXTENSIONS
					draw_splitbar(hwnd, last_split);
#else
					child->split_pos = last_split;
#endif
					GetClientRect(hwnd, &rt);
					resize_tree(child, rt.right, rt.bottom);
					last_split = -1;
					ReleaseCapture();
					SetCursor(LoadCursor(0, IDC_ARROW));
				}
			break;

		case WM_MOUSEMOVE:
			if (GetCapture() == hwnd) {
				RECT rt;
4382
                                int x = (short)LOWORD(lparam);
4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420

#ifdef _NO_EXTENSIONS
				HDC hdc = GetDC(hwnd);
				GetClientRect(hwnd, &rt);

				rt.left = last_split-SPLIT_WIDTH/2;
				rt.right = last_split+SPLIT_WIDTH/2+1;
				InvertRect(hdc, &rt);

				last_split = x;
				rt.left = x-SPLIT_WIDTH/2;
				rt.right = x+SPLIT_WIDTH/2+1;
				InvertRect(hdc, &rt);

				ReleaseDC(hwnd, hdc);
#else
				GetClientRect(hwnd, &rt);

				if (x>=0 && x<rt.right) {
					child->split_pos = x;
					resize_tree(child, rt.right, rt.bottom);
					rt.left = x-SPLIT_WIDTH/2;
					rt.right = x+SPLIT_WIDTH/2+1;
					InvalidateRect(hwnd, &rt, FALSE);
					UpdateWindow(child->left.hwnd);
					UpdateWindow(hwnd);
					UpdateWindow(child->right.hwnd);
				}
#endif
			}
			break;

#ifndef _NO_EXTENSIONS
		case WM_GETMINMAXINFO:
			DefMDIChildProc(hwnd, nmsg, wparam, lparam);

			{LPMINMAXINFO lpmmi = (LPMINMAXINFO)lparam;

4421 4422
			lpmmi->ptMaxTrackSize.x <<= 1;/*2*GetSystemMetrics(SM_CXSCREEN) / SM_CXVIRTUALSCREEN */
			lpmmi->ptMaxTrackSize.y <<= 1;/*2*GetSystemMetrics(SM_CYSCREEN) / SM_CYVIRTUALSCREEN */
4423
			break;}
4424
#endif /* _NO_EXTENSIONS */
4425 4426

		case WM_SETFOCUS:
4427 4428
			if (SetCurrentDirectory(child->path))
				set_space_status();
4429 4430 4431 4432 4433 4434 4435 4436
			SetFocus(child->focus_pane? child->right.hwnd: child->left.hwnd);
			break;

		case WM_DISPATCH_COMMAND: {
			Pane* pane = GetFocus()==child->left.hwnd? &child->left: &child->right;

			switch(LOWORD(wparam)) {
				case ID_WINDOW_NEW: {
4437
					ChildWnd* new_child = alloc_child_window(child->path, NULL, hwnd);
4438 4439

					if (!create_child_window(new_child))
4440
						HeapFree(GetProcessHeap(), 0, new_child);
4441 4442 4443 4444

					break;}

				case ID_REFRESH:
4445
					refresh_drives();
4446
					refresh_child(child);
4447 4448 4449
					break;

				case ID_ACTIVATE:
4450
					activate_entry(child, pane, hwnd);
4451 4452
					break;

Piotr Caban's avatar
Piotr Caban committed
4453
				case ID_FILE_MOVE: {
4454
					TCHAR source[BUFFER_LEN], target[BUFFER_LEN];
4455

4456 4457
					if (prompt_target(pane, source, target)) {
						SHFILEOPSTRUCT shfo = {hwnd, FO_MOVE, source, target};
4458

4459 4460
						source[lstrlen(source)+1] = '\0';
						target[lstrlen(target)+1] = '\0';
4461

4462 4463 4464 4465
						if (!SHFileOperation(&shfo))
							refresh_child(child);
					}
					break;}
4466

4467 4468
				case ID_FILE_COPY: {
					TCHAR source[BUFFER_LEN], target[BUFFER_LEN];
4469

4470 4471
					if (prompt_target(pane, source, target)) {
						SHFILEOPSTRUCT shfo = {hwnd, FO_COPY, source, target};
4472

4473 4474
						source[lstrlen(source)+1] = '\0';
						target[lstrlen(target)+1] = '\0';
4475

4476
						if (!SHFileOperation(&shfo))
4477
							refresh_child(child);
Piotr Caban's avatar
Piotr Caban committed
4478
					}
4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490
					break;}

				case ID_FILE_DELETE: {
					TCHAR path[BUFFER_LEN];
					SHFILEOPSTRUCT shfo = {hwnd, FO_DELETE, path};

					get_path(pane->cur, path);

					path[lstrlen(path)+1] = '\0';

					if (!SHFileOperation(&shfo))
						refresh_child(child);
Piotr Caban's avatar
Piotr Caban committed
4491 4492
					break;}

4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508
				case ID_VIEW_SORT_NAME:
					set_sort_order(child, SORT_NAME);
					break;

				case ID_VIEW_SORT_TYPE:
					set_sort_order(child, SORT_EXT);
					break;

				case ID_VIEW_SORT_SIZE:
					set_sort_order(child, SORT_SIZE);
					break;

				case ID_VIEW_SORT_DATE:
					set_sort_order(child, SORT_DATE);
					break;

4509 4510 4511 4512
				case ID_VIEW_FILTER: {
					struct FilterDialog dlg;

					memset(&dlg, 0, sizeof(struct FilterDialog));
4513
					lstrcpy(dlg.pattern, child->filter_pattern);
4514 4515 4516
					dlg.flags = child->filter_flags;

					if (DialogBoxParam(Globals.hInstance, MAKEINTRESOURCE(IDD_DIALOG_VIEW_TYPE), hwnd, FilterDialogDlgProc, (LPARAM)&dlg) == IDOK) {
4517
						lstrcpy(child->filter_pattern, dlg.pattern);
4518 4519 4520 4521 4522
						child->filter_flags = dlg.flags;
						refresh_right_pane(child);
					}
					break;}

4523 4524 4525 4526 4527 4528 4529 4530
				case ID_VIEW_SPLIT: {
					last_split = child->split_pos;
#ifdef _NO_EXTENSIONS
					draw_splitbar(hwnd, last_split);
#endif
					SetCapture(hwnd);
					break;}

4531 4532 4533 4534
				case ID_EDIT_PROPERTIES:
					show_properties_dlg(pane->cur, child->hwnd);
					break;

4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545
				default:
					return pane_command(pane, LOWORD(wparam));
			}

			return TRUE;}

		case WM_COMMAND: {
			Pane* pane = GetFocus()==child->left.hwnd? &child->left: &child->right;

			switch(HIWORD(wparam)) {
				case LBN_SELCHANGE: {
4546 4547
					int idx = SendMessage(pane->hwnd, LB_GETCURSEL, 0, 0);
					Entry* entry = (Entry*) SendMessage(pane->hwnd, LB_GETITEMDATA, idx, 0);
4548 4549

					if (pane == &child->left)
4550
						set_curdir(child, entry, idx, hwnd);
4551 4552 4553 4554 4555
					else
						pane->cur = entry;
					break;}

				case LBN_DBLCLK:
4556
					activate_entry(child, pane, hwnd);
4557 4558 4559 4560 4561 4562 4563 4564 4565 4566
					break;
			}
			break;}

#ifndef _NO_EXTENSIONS
		case WM_NOTIFY: {
			NMHDR* pnmh = (NMHDR*) lparam;
			return pane_notify(pnmh->idFrom==IDW_HEADER_LEFT? &child->left: &child->right, pnmh);}
#endif

4567 4568
#ifdef _SHELL_FOLDERS
		case WM_CONTEXTMENU: {
4569
			POINT pt, pt_clnt;
4570 4571 4572 4573 4574
			Pane* pane;
			int idx;

			 /* first select the current item in the listbox */
			HWND hpanel = (HWND) wparam;
4575 4576 4577 4578 4579
			pt_clnt.x = pt.x = (short)LOWORD(lparam);
			pt_clnt.y = pt.y = (short)HIWORD(lparam);
			ScreenToClient(hpanel, &pt_clnt);
			SendMessage(hpanel, WM_LBUTTONDOWN, 0, MAKELONG(pt_clnt.x, pt_clnt.y));
			SendMessage(hpanel, WM_LBUTTONUP, 0, MAKELONG(pt_clnt.x, pt_clnt.y));
4580 4581 4582

			 /* now create the popup menu using shell namespace and IContextMenu */
			pane = GetFocus()==child->left.hwnd? &child->left: &child->right;
4583
			idx = SendMessage(pane->hwnd, LB_GETCURSEL, 0, 0);
4584 4585

			if (idx != -1) {
4586
				Entry* entry = (Entry*) SendMessage(pane->hwnd, LB_GETITEMDATA, idx, 0);
4587 4588 4589 4590 4591 4592 4593 4594 4595

				LPITEMIDLIST pidl_abs = get_to_absolute_pidl(entry, hwnd);

				if (pidl_abs) {
					IShellFolder* parentFolder;
					LPCITEMIDLIST pidlLast;

					 /* get and use the parent folder to display correct context menu in all cases */
					if (SUCCEEDED(SHBindToParent(pidl_abs, &IID_IShellFolder, (LPVOID*)&parentFolder, &pidlLast))) {
4596
						if (ShellFolderContextMenu(parentFolder, hwnd, 1, &pidlLast, pt.x, pt.y) == S_OK)
4597
							refresh_child(child);
4598

4599
						IShellFolder_Release(parentFolder);
4600 4601
					}

4602
					IMalloc_Free(Globals.iMalloc, pidl_abs);
4603 4604 4605 4606 4607
				}
			}
			break;}
#endif

4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619
		  case WM_MEASUREITEM:
		  draw_menu_item:
			if (!wparam)	/* Is the message menu-related? */
				if (CtxMenu_HandleMenuMsg(nmsg, wparam, lparam))
					return TRUE;

			break;

		  case WM_INITMENUPOPUP:
			if (CtxMenu_HandleMenuMsg(nmsg, wparam, lparam))
				return 0;

4620
			update_view_menu(child);
4621 4622 4623 4624 4625 4626
			break;

		  case WM_MENUCHAR:	/* only supported by IContextMenu3 */
		   if (s_pctxmenu3) {
			   LRESULT lResult = 0;

4627
			   IContextMenu3_HandleMenuMsg2(s_pctxmenu3, nmsg, wparam, lparam, &lResult);
4628 4629 4630 4631 4632 4633

			   return lResult;
		   }

		   break;

4634 4635 4636
		case WM_SIZE:
			if (wparam != SIZE_MINIMIZED)
				resize_tree(child, LOWORD(lparam), HIWORD(lparam));
4637
			/* fall through */
4638 4639 4640 4641 4642 4643 4644 4645 4646

		default: def:
			return DefMDIChildProc(hwnd, nmsg, wparam, lparam);
	}

	return 0;
}


Mike McCormack's avatar
Mike McCormack committed
4647
static LRESULT CALLBACK TreeWndProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam)
4648
{
4649 4650
	ChildWnd* child = (ChildWnd*) GetWindowLongPtr(GetParent(hwnd), GWLP_USERDATA);
	Pane* pane = (Pane*) GetWindowLongPtr(hwnd, GWLP_USERDATA);
4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661
	ASSERT(child);

	switch(nmsg) {
#ifndef _NO_EXTENSIONS
		case WM_HSCROLL:
			set_header(pane);
			break;
#endif

		case WM_SETFOCUS:
			child->focus_pane = pane==&child->right? 1: 0;
4662
			SendMessage(hwnd, LB_SETSEL, TRUE, 1);
4663
			/*TODO: check menu items */
4664 4665 4666 4667
			break;

		case WM_KEYDOWN:
			if (wparam == VK_TAB) {
4668
				/*TODO: SetFocus(Globals.hdrivebar) */
4669 4670 4671 4672 4673 4674 4675 4676 4677 4678
				SetFocus(child->focus_pane? child->left.hwnd: child->right.hwnd);
			}
	}

	return CallWindowProc(g_orgTreeWndProc, hwnd, nmsg, wparam, lparam);
}


static void InitInstance(HINSTANCE hinstance)
{
4679
	static const TCHAR sFont[] = {'M','i','c','r','o','s','o','f','t',' ','S','a','n','s',' ','S','e','r','i','f','\0'};
4680

4681 4682
	WNDCLASSEX wcFrame;
	WNDCLASS wcChild;
4683
	ATOM hChildClass;
4684
	int col;
4685 4686 4687 4688 4689 4690 4691 4692

	INITCOMMONCONTROLSEX icc = {
		sizeof(INITCOMMONCONTROLSEX),
		ICC_BAR_CLASSES
	};

	HDC hdc = GetDC(0);

4693 4694 4695 4696 4697 4698
	setlocale(LC_COLLATE, "");	/* set collating rules to local settings for compareName */

	InitCommonControlsEx(&icc);


	/* register frame window class */
4699 4700 4701 4702 4703 4704 4705

	wcFrame.cbSize        = sizeof(WNDCLASSEX);
	wcFrame.style         = 0;
	wcFrame.lpfnWndProc   = FrameWndProc;
	wcFrame.cbClsExtra    = 0;
	wcFrame.cbWndExtra    = 0;
	wcFrame.hInstance     = hinstance;
4706
	wcFrame.hIcon         = LoadIcon(hinstance, MAKEINTRESOURCE(IDI_WINEFILE));
4707 4708 4709
	wcFrame.hCursor       = LoadCursor(0, IDC_ARROW);
	wcFrame.hbrBackground = 0;
	wcFrame.lpszMenuName  = 0;
4710
	wcFrame.lpszClassName = sWINEFILEFRAME;
4711
	wcFrame.hIconSm       = (HICON)LoadImage(hinstance,
4712 4713 4714 4715 4716
											 MAKEINTRESOURCE(IDI_WINEFILE),
											 IMAGE_ICON,
											 GetSystemMetrics(SM_CXSMICON),
											 GetSystemMetrics(SM_CYSMICON),
											 LR_SHARED);
4717

4718 4719 4720 4721
	Globals.hframeClass = RegisterClassEx(&wcFrame);


	/* register tree windows class */
4722 4723 4724 4725 4726 4727 4728 4729 4730 4731

	wcChild.style         = CS_CLASSDC|CS_DBLCLKS|CS_VREDRAW;
	wcChild.lpfnWndProc   = ChildWndProc;
	wcChild.cbClsExtra    = 0;
	wcChild.cbWndExtra    = 0;
	wcChild.hInstance     = hinstance;
	wcChild.hIcon         = 0;
	wcChild.hCursor       = LoadCursor(0, IDC_ARROW);
	wcChild.hbrBackground = 0;
	wcChild.lpszMenuName  = 0;
4732
	wcChild.lpszClassName = sWINEFILETREE;
4733 4734 4735

	hChildClass = RegisterClass(&wcChild);

4736 4737 4738

	Globals.haccel = LoadAccelerators(hinstance, MAKEINTRESOURCE(IDA_WINEFILE));

4739
	Globals.hfont = CreateFont(-MulDiv(8,GetDeviceCaps(hdc,LOGPIXELSY),72), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, sFont);
4740 4741 4742 4743 4744

	ReleaseDC(0, hdc);

	Globals.hInstance = hinstance;

4745 4746 4747 4748
#ifdef _SHELL_FOLDERS
	CoInitialize(NULL);
	CoGetMalloc(MEMCTX_TASK, &Globals.iMalloc);
	SHGetDesktopFolder(&Globals.iDesktop);
4749 4750 4751
#ifdef __WINE__
	Globals.cfStrFName = RegisterClipboardFormatA(CFSTR_FILENAME);
#else
4752 4753
	Globals.cfStrFName = RegisterClipboardFormat(CFSTR_FILENAME);
#endif
4754 4755 4756
#endif

	/* load column strings */
4757
	col = 1;
4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771

	load_string(g_pos_names[col++], IDS_COL_NAME);
	load_string(g_pos_names[col++], IDS_COL_SIZE);
	load_string(g_pos_names[col++], IDS_COL_CDATE);
#ifndef _NO_EXTENSIONS
	load_string(g_pos_names[col++], IDS_COL_ADATE);
	load_string(g_pos_names[col++], IDS_COL_MDATE);
	load_string(g_pos_names[col++], IDS_COL_IDX);
	load_string(g_pos_names[col++], IDS_COL_LINKS);
#endif
	load_string(g_pos_names[col++], IDS_COL_ATTR);
#ifndef _NO_EXTENSIONS
	load_string(g_pos_names[col++], IDS_COL_SEC);
#endif
4772 4773 4774
}


4775
static void show_frame(HWND hwndParent, int cmdshow, LPCTSTR path)
4776
{
4777
	static const TCHAR sMDICLIENT[] = {'M','D','I','C','L','I','E','N','T','\0'};
4778

4779
	TCHAR buffer[MAX_PATH], b1[BUFFER_LEN];
4780 4781
	ChildWnd* child;
	HMENU hMenuFrame, hMenuWindow;
4782
	windowOptions opts;
4783 4784 4785 4786 4787 4788

	CLIENTCREATESTRUCT ccs;

	if (Globals.hMainWnd)
		return;

4789
	opts = load_registry_settings();
4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800
	hMenuFrame = LoadMenu(Globals.hInstance, MAKEINTRESOURCE(IDM_WINEFILE));
	hMenuWindow = GetSubMenu(hMenuFrame, GetMenuItemCount(hMenuFrame)-2);

	Globals.hMenuFrame = hMenuFrame;
	Globals.hMenuView = GetSubMenu(hMenuFrame, 3);
	Globals.hMenuOptions = GetSubMenu(hMenuFrame, 4);

	ccs.hWindowMenu  = hMenuWindow;
	ccs.idFirstChild = IDW_FIRST_CHILD;


4801
	/* create main window */
4802
	Globals.hMainWnd = CreateWindowEx(0, (LPCTSTR)(int)Globals.hframeClass, RS(b1,IDS_WINE_FILE), WS_OVERLAPPEDWINDOW,
4803
					opts.start_x, opts.start_y, opts.width, opts.height,
4804
					hwndParent, Globals.hMenuFrame, Globals.hInstance, 0/*lpParam*/);
4805 4806


4807
	Globals.hmdiclient = CreateWindowEx(0, sMDICLIENT, NULL,
4808 4809
					WS_CHILD|WS_CLIPCHILDREN|WS_VSCROLL|WS_HSCROLL|WS_VISIBLE|WS_BORDER,
					0, 0, 0, 0,
4810
					Globals.hMainWnd, 0, Globals.hInstance, &ccs);
4811
  
4812
	CheckMenuItem(Globals.hMenuOptions, ID_VIEW_DRIVE_BAR, MF_BYCOMMAND|MF_CHECKED);
4813
	CheckMenuItem(Globals.hMenuOptions, ID_VIEW_SAVESETTINGS, MF_BYCOMMAND);
4814

4815
	create_drive_bar();
4816 4817 4818

	{
		TBBUTTON toolbarBtns[] = {
4819 4820 4821 4822 4823
			{0, 0, 0, BTNS_SEP, {0, 0}, 0, 0},
			{0, ID_WINDOW_NEW, TBSTATE_ENABLED, BTNS_BUTTON, {0, 0}, 0, 0},
			{1, ID_WINDOW_CASCADE, TBSTATE_ENABLED, BTNS_BUTTON, {0, 0}, 0, 0},
			{2, ID_WINDOW_TILE_HORZ, TBSTATE_ENABLED, BTNS_BUTTON, {0, 0}, 0, 0},
			{3, ID_WINDOW_TILE_VERT, TBSTATE_ENABLED, BTNS_BUTTON, {0, 0}, 0, 0},
4824
/*TODO
4825 4826
			{4, ID_... , TBSTATE_ENABLED, BTNS_BUTTON, {0, 0}, 0, 0},
			{5, ID_... , TBSTATE_ENABLED, BTNS_BUTTON, {0, 0}, 0, 0},
4827
*/		};
4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842

		Globals.htoolbar = CreateToolbarEx(Globals.hMainWnd, WS_CHILD|WS_VISIBLE,
			IDW_TOOLBAR, 2, Globals.hInstance, IDB_TOOLBAR, toolbarBtns,
			sizeof(toolbarBtns)/sizeof(TBBUTTON), 16, 15, 16, 15, sizeof(TBBUTTON));
		CheckMenuItem(Globals.hMenuOptions, ID_VIEW_TOOL_BAR, MF_BYCOMMAND|MF_CHECKED);
	}

	Globals.hstatusbar = CreateStatusWindow(WS_CHILD|WS_VISIBLE, 0, Globals.hMainWnd, IDW_STATUSBAR);
	CheckMenuItem(Globals.hMenuOptions, ID_VIEW_STATUSBAR, MF_BYCOMMAND|MF_CHECKED);

/* CreateStatusWindow does not accept WS_BORDER
	Globals.hstatusbar = CreateWindowEx(WS_EX_NOPARENTNOTIFY, STATUSCLASSNAME, 0,
					WS_CHILD|WS_VISIBLE|WS_CLIPSIBLINGS|WS_BORDER|CCS_NODIVIDER, 0,0,0,0,
					Globals.hMainWnd, (HMENU)IDW_STATUSBAR, hinstance, 0);*/

4843
	/*TODO: read paths from registry */
4844 4845 4846 4847 4848

	if (!path || !*path) {
		GetCurrentDirectory(MAX_PATH, buffer);
		path = buffer;
	}
4849 4850 4851

	ShowWindow(Globals.hMainWnd, cmdshow);

4852
#if defined(_SHELL_FOLDERS) && !defined(__WINE__)
4853 4854 4855 4856 4857
	 /* Shell Namespace as default: */
	child = alloc_child_window(path, get_path_pidl(path,Globals.hMainWnd), Globals.hMainWnd);
#else
	child = alloc_child_window(path, NULL, Globals.hMainWnd);
#endif
4858 4859 4860 4861 4862 4863 4864 4865

	child->pos.showCmd = SW_SHOWMAXIMIZED;
	child->pos.rcNormalPosition.left = 0;
	child->pos.rcNormalPosition.top = 0;
	child->pos.rcNormalPosition.right = 320;
	child->pos.rcNormalPosition.bottom = 280;

	if (!create_child_window(child))
4866
		HeapFree(GetProcessHeap(), 0, child);
4867 4868 4869 4870 4871 4872

	SetWindowPlacement(child->hwnd, &child->pos);

	Globals.himl = ImageList_LoadBitmap(Globals.hInstance, MAKEINTRESOURCE(IDB_IMAGES), 16, 0, RGB(0,255,0));

	Globals.prescan_node = FALSE;
4873 4874

	UpdateWindow(Globals.hMainWnd);
4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886

	if (path && path[0])
	{
		int index,count;
		TCHAR drv[_MAX_DRIVE+1], dir[_MAX_DIR], name[_MAX_FNAME], ext[_MAX_EXT];
		TCHAR fullname[_MAX_FNAME+_MAX_EXT+1];

		memset(name,0,sizeof(name));
		memset(name,0,sizeof(ext));
		_tsplitpath(path, drv, dir, name, ext);
		if (name[0])
		{
4887
			count = SendMessage(child->right.hwnd, LB_GETCOUNT, 0, 0);
4888 4889 4890 4891 4892
			lstrcpy(fullname,name);
			lstrcat(fullname,ext);

			for (index = 0; index < count; index ++)
			{
4893
				Entry* entry = (Entry*) SendMessage(child->right.hwnd, LB_GETITEMDATA, index, 0);
4894 4895 4896
				if (lstrcmp(entry->data.cFileName,fullname)==0 ||
						lstrcmp(entry->data.cAlternateFileName,fullname)==0)
				{
4897
					SendMessage(child->right.hwnd, LB_SETCURSEL, index, 0);
4898 4899 4900 4901 4902 4903
					SetFocus(child->right.hwnd);
					break;
				}
			}
		}
	}
4904 4905
}

Mike McCormack's avatar
Mike McCormack committed
4906
static void ExitInstance(void)
4907
{
4908
#ifdef _SHELL_FOLDERS
4909 4910
	IShellFolder_Release(Globals.iDesktop);
	IMalloc_Release(Globals.iMalloc);
4911 4912 4913
	CoUninitialize();
#endif

4914
	DeleteObject(Globals.hfont);
4915 4916 4917
	ImageList_Destroy(Globals.himl);
}

Mike McCormack's avatar
Mike McCormack committed
4918
#ifdef _NO_EXTENSIONS
4919

4920
/* search for already running win[e]files */
4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937

static int g_foundPrevInstance = 0;

static BOOL CALLBACK EnumWndProc(HWND hwnd, LPARAM lparam)
{
	TCHAR cls[128];

	GetClassName(hwnd, cls, 128);

	if (!lstrcmp(cls, (LPCTSTR)lparam)) {
		g_foundPrevInstance++;
		return FALSE;
	}

	return TRUE;
}

4938
/* search for window of given class name to allow only one running instance */
Mike McCormack's avatar
Mike McCormack committed
4939
static int find_window_class(LPCTSTR classname)
4940
{
4941
	EnumWindows(EnumWndProc, (LPARAM)classname);
4942 4943 4944

	if (g_foundPrevInstance)
		return 1;
4945 4946 4947 4948

	return 0;
}

Mike McCormack's avatar
Mike McCormack committed
4949
#endif
4950

4951
static int winefile_main(HINSTANCE hinstance, int cmdshow, LPCTSTR path)
4952 4953
{
	MSG msg;
4954
  
4955 4956
	InitInstance(hinstance);

4957
	show_frame(0, cmdshow, path);
4958 4959

	while(GetMessage(&msg, 0, 0, 0)) {
4960 4961 4962 4963 4964 4965 4966 4967
		if (Globals.hmdiclient && TranslateMDISysAccel(Globals.hmdiclient, &msg))
			continue;

		if (Globals.hMainWnd && TranslateAccelerator(Globals.hMainWnd, Globals.haccel, &msg))
			continue;

		TranslateMessage(&msg);
		DispatchMessage(&msg);
4968 4969 4970 4971
	}

	ExitInstance();

4972 4973 4974 4975
	return msg.wParam;
}


4976 4977 4978 4979 4980
#if defined(UNICODE) && defined(_MSC_VER)
int APIENTRY wWinMain(HINSTANCE hinstance, HINSTANCE previnstance, LPWSTR cmdline, int cmdshow)
#else
int APIENTRY WinMain(HINSTANCE hinstance, HINSTANCE previnstance, LPSTR cmdline, int cmdshow)
#endif
4981 4982
{
#ifdef _NO_EXTENSIONS
4983
	if (find_window_class(sWINEFILEFRAME))
4984 4985 4986
		return 1;
#endif

4987 4988 4989 4990 4991 4992 4993 4994 4995
#if defined(UNICODE) && !defined(_MSC_VER)
	{ /* convert ANSI cmdline into WCS path string */
	TCHAR buffer[MAX_PATH];
	MultiByteToWideChar(CP_ACP, 0, cmdline, -1, buffer, MAX_PATH);
	winefile_main(hinstance, cmdshow, buffer);
	}
#else
	winefile_main(hinstance, cmdshow, cmdline);
#endif
4996

4997 4998
	return 0;
}