aboutsummaryrefslogtreecommitdiff
path: root/Src/Winamp/OpenFileDialog.cpp
blob: d25c0538c0ef5dafe9f5af6f7e241189ce9ddc7f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/** (c) Nullsoft, Inc.         C O N F I D E N T I A L
 ** Filename:
 ** Project:
 ** Description:
 ** Author:
 ** Created:
 **/

#include "main.h"
#include "resource.h"
#include "strutil.h"
#include "../nu/AutoChar.h"
#include "api.h"
#include <shlwapi.h>
#include <shobjidl.h>

/*static wchar_t *inc(wchar_t *p, size_t &size, int extra=0)
{
	int len = lstrlenW(p) + extra;
	size-=len;
	p+=len;
	return p;
}*/

#if 0
extern std::vector<In_Module*> in_modules;
void getNewFileVistaPlus(int clearlist, HWND hwnd, const wchar_t *path)
{
    IFileOpenDialog *pFileOpen;
	static int qq;
	if (qq) return;
	qq = 1;
    HRESULT hr = CoCreateInstance(__uuidof(FileOpenDialog), NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pFileOpen));
    if (SUCCEEDED(hr))
    {
		wchar_t titleStr[128] = {0};

		pFileOpen->SetTitle(getStringW((clearlist ? IDS_OFD_OPEN_FILES : IDS_OFD_ADD_FILES_TO_PLAYLIST),titleStr,128));
		pFileOpen->SetDefaultExtension(L"");
		pFileOpen->SetOptions(FOS_FILEMUSTEXIST | FOS_ALLOWMULTISELECT | FOS_NOREADONLYRETURN | FOS_PATHMUSTEXIST);
		if(!clearlist) pFileOpen->SetOkButtonLabel(L"Add");

		IShellItem* shellItem;
		hr = SHCreateItemFromParsingName((!path ? WASABI_API_APP->path_getWorkingPath() : path), 0, IID_IShellItem, reinterpret_cast<void**>(&shellItem));
		if (SUCCEEDED(hr))
		{
			pFileOpen->SetFolder(shellItem);
		}

		wchar_t *fsb = in_getfltstrW(TRUE);
		int filterNum = 0;
		// allocate enough for the number of input plug-ins plus 'all supported' and 'all files'
		COMDLG_FILTERSPEC *fileTypes = new COMDLG_FILTERSPEC[in_modules.size() + 2];
		while(fsb && *fsb && *fsb+1)
		{
			int len = lstrlenW(fsb) + 1;
			wchar_t *fsb2 = fsb + len;
            fileTypes[filterNum].pszName = fsb;
            fileTypes[filterNum].pszSpec = fsb2;
			CharUpperBuffW(fsb2, lstrlenW(fsb2));
			filterNum++;
			fsb += len + lstrlenW(fsb2) + 1;
		}

		pFileOpen->SetFileTypes(filterNum, fileTypes);

		if (hwnd == hMainWindow)
			UninitDragDrops();

        hr = pFileOpen->Show(hwnd);
        if (SUCCEEDED(hr))
        {
			IShellItemArray *pItems;
			hr = pFileOpen->GetResults(&pItems);
            if (SUCCEEDED(hr))
            {
				IEnumShellItems *sItems;
				hr = pItems->EnumItems(&sItems);
				if (SUCCEEDED(hr))
				{
					DWORD numItems = 0;
					pItems->GetCount(&numItems);

					int sp;
					if (clearlist)
					{
						sp = 0; PlayList_delete();
					}
					else sp = PlayList_getlength();

					if(numItems <= 1)
					{
						IShellItem *pItem;
						if(sItems->Next(1, &pItem, NULL) == S_OK)
						{
							LPWSTR folderpath = NULL;
							LPWSTR filepath = NULL;
							hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &filepath);
							if (SUCCEEDED(hr))
							{
								IShellItem *ppItem;
								hr = pItem->GetParent(&ppItem);
								if (SUCCEEDED(hr))
								{
									hr = ppItem->GetDisplayName(SIGDN_FILESYSPATH, &folderpath);
									if (SUCCEEDED(hr))
									{
										WASABI_API_APP->path_setWorkingPath(folderpath);
									}
								}

								if (LoadPlaylist(filepath, 1, 0) == -1) // if not a playlist file
								{
									PlayList_append(filepath, 0);
								}

								CoTaskMemFree(filepath);
								CoTaskMemFree(folderpath);
							}
						}
						pItem->Release();
					}
					else
					{
						size_t size = ((numItems + 1) * MAX_PATH);
						wchar_t *temp = (wchar_t *)GlobalAlloc(GPTR, size * sizeof(wchar_t)), *p = temp;
						IShellItem *pItem;
						while(sItems->Next(1, &pItem, NULL) == S_OK)
						{
							LPWSTR folderpath = NULL;
							LPWSTR filepath = NULL;
							hr = pItem->GetDisplayName(SIGDN_NORMALDISPLAY, &filepath);
							if (SUCCEEDED(hr))
							{
								// replicate how lpstrFile is filled under a multiple select
								// -> selected folder followed by each filename \0 separated
								if(p == temp)
								{
									IShellItem *ppItem;
									hr = pItem->GetParent(&ppItem);
									if (SUCCEEDED(hr))
									{
										hr = ppItem->GetDisplayName(SIGDN_FILESYSPATH, &folderpath);
										if (SUCCEEDED(hr))
										{
											WASABI_API_APP->path_setWorkingPath(folderpath);

											if(StringCchCatW(p, size, folderpath) == S_OK)
											{
												int len = lstrlenW(folderpath) + 1;
												p += len;
												size -= len;
											}
										}
									}
								}

								if(StringCchCatW(p, size, filepath) == S_OK)
								{
									int len = lstrlenW(filepath) + 1;
									p += len;
									size -= len;
								}

								CoTaskMemFree(filepath);
								CoTaskMemFree(folderpath);
							}
							pItem->Release();
						}

						PlayList_addfromdlg(temp);
						if (config_rofiob&1) PlayList_sort(2, sp);

						GlobalFree(temp);
					}

					if (clearlist)
						BeginPlayback();

					sItems->Release();
				}
				pItems->Release();
            }
        }
        pFileOpen->Release();
		GlobalFree(fsb);

		if (hwnd == hMainWindow)
			InitDragDrops();
		for(;;)
		{
			MSG msg;
			if (!PeekMessage(&msg, hMainWindow, WM_MOUSEFIRST, WM_MOUSELAST, PM_REMOVE))
				break;
		}
    }
	qq = 0;
}
#endif

void getNewFile(int clearlist, HWND hwnd, const wchar_t *path) // if clearlist is 1, playlist is cleared
{
	// TODO - Egg was having stability issues with this so keeping disabled until resolved
	// on Vista and up use the IFileOpenDialog interface as it
	// allows us to get around the MAX_PATH file filter issues
	/*if(IsVista())
	{
		getNewFileVistaPlus(clearlist, hwnd, path);
		return;
	}*/

	// otherwise revert to using the older pre-Vista methods
	const int len = 256 * 1024 - 128;

	wchar_t oldCurPath[MAX_PATH] = {0}, titleStr[128] = {0};

	GetCurrentDirectoryW(MAX_PATH, oldCurPath);
	wchar_t *temp, *fsb;
	OPENFILENAMEW l = {sizeof(OPENFILENAMEW),0};
	static int q;
	if (q) return;
	q = 1;
	temp = (wchar_t *)GlobalAlloc(GPTR, len * sizeof(wchar_t));
	l.lStructSize = sizeof(l);
	l.hwndOwner = hwnd;
	l.lpstrFilter = fsb = in_getfltstrW(FALSE);

	l.lpstrFile = temp;
	l.nMaxFile = len - 1;
	l.lpstrTitle = getStringW((clearlist ? IDS_OFD_OPEN_FILES : IDS_OFD_ADD_FILES_TO_PLAYLIST),titleStr,128);
	l.lpstrDefExt = L"";
	l.lpstrInitialDir = path;
	if (!l.lpstrInitialDir) l.lpstrInitialDir = WASABI_API_APP->path_getWorkingPath();

	l.Flags = OFN_HIDEREADONLY | OFN_EXPLORER | OFN_ALLOWMULTISELECT
	          | OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST;
	if (hwnd == hMainWindow)
		UninitDragDrops();
	if (GetOpenFileNameW(&l))
	{
		wchar_t newCurPath[MAX_PATH] = {0};
		GetCurrentDirectoryW(MAX_PATH, newCurPath);
		WASABI_API_APP->path_setWorkingPath(newCurPath);
		int sp;
		if (clearlist)
		{
			sp = 0; PlayList_delete();
		}
		else sp = PlayList_getlength();
		if (temp[lstrlenW(temp)+1])
		{
			PlayList_addfromdlg(temp);
			if (config_rofiob&1) PlayList_sort(2, sp);
		}
		else
		{
			if (LoadPlaylist(temp, 1, 0) == -1) // if not a playlist file
			{
				PlayList_append(temp, 0);
			}
		}
		if (clearlist)
			BeginPlayback();
	}
	SetCurrentDirectoryW(oldCurPath);

	if (hwnd == hMainWindow)
		InitDragDrops();
	for(;;)
	{
		MSG msg;
		if (!PeekMessage(&msg, hMainWindow, WM_MOUSEFIRST, WM_MOUSELAST, PM_REMOVE))
			break;
	}
	GlobalFree(fsb);
	GlobalFree(temp);
	q = 0;
}

static wchar_t loc[FILENAME_SIZE];
int ResizeComboBoxDropDown(HWND hwndDlg, UINT id, const char* str, int width){
	SIZE size = {0};
	HWND control = GetDlgItem(hwndDlg, id);
	HDC hdc = GetDC(control);
	// get and select parent dialog's font so that it'll calculate things correctly
	HFONT font = (HFONT)SendMessageW(hwndDlg,WM_GETFONT,0,0), oldfont = (HFONT)SelectObject(hdc,font);
	GetTextExtentPoint32A(hdc, str, lstrlenA(str)+1, &size);

	int ret = width;
	if(size.cx > width)
	{
		SendDlgItemMessage(hwndDlg, id, CB_SETDROPPEDWIDTH, size.cx, 0);
		ret = size.cx;
	}

	SelectObject(hdc, oldfont);
	ReleaseDC(control, hdc);
	return ret;
}

int ResizeComboBoxDropDownW(HWND hwndDlg, UINT id, const wchar_t *str, int width){
	SIZE size = {0};
	HWND control = GetDlgItem(hwndDlg, id);
	HDC hdc = GetDC(control);
	// get and select parent dialog's font so that it'll calculate things correctly
	HFONT font = (HFONT)SendMessageW(hwndDlg,WM_GETFONT,0,0), oldfont = (HFONT)SelectObject(hdc,font);
	GetTextExtentPoint32W(hdc, str, (int) wcslen(str)+1, &size);

	int ret = width;
	if(size.cx > width)
	{
		SendDlgItemMessage(hwndDlg, id, CB_SETDROPPEDWIDTH, size.cx, 0);
		ret = size.cx;
	}

	SelectObject(hdc, oldfont);
	ReleaseDC(control, hdc);
	return ret;
}

static LRESULT WINAPI locProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch (uMsg)
	{
		case WM_INITDIALOG:
			if(lParam) SetWindowTextW(hwndDlg, getStringW(IDS_ADD_URL, NULL, 0));
			SendDlgItemMessageW(hwndDlg, IDC_URL_NEW, CB_LIMITTEXT, sizeof(loc)/sizeof(*loc) - 1, 0);
			{
				int width = 0, x = 0;
				HWND h = GetDlgItem(hwndDlg, IDC_URL_NEW);
				SendMessageW(h, CB_SETHORIZONTALEXTENT, 400, 0);
				while(1)
				{
					char s[123] = {0};
					wchar_t name[FILENAME_SIZE] = {0};
					StringCchPrintfA(s, 123, "RecentURL%d", x + 1);
					_r_sW(s, name, 123);
					if (!name[0]) break;
					SendMessageW(h, CB_ADDSTRING, 0, (LPARAM)name);
					width = ResizeComboBoxDropDownW(hwndDlg, IDC_URL_NEW, name, width);
					x++;
				}

				// show add / open loc window and restore last position as applicable
				POINT pt = {loc_rect.left, loc_rect.top};
				if (!windowOffScreen(hwndDlg, pt))
					SetWindowPos(hwndDlg, HWND_TOP, loc_rect.left, loc_rect.top, 0, 0, SWP_NOSIZE | SWP_NOSENDCHANGING);
			}

			SetDlgItemTextW(hwndDlg, IDC_URL_NEW, loc);
			return TRUE;
		case WM_COMMAND:
			switch (LOWORD(wParam))
			{
				case IDOK:
					GetDlgItemTextW(hwndDlg, IDC_URL_NEW, loc, sizeof(loc)/sizeof(*loc) - 1);
					{
						wchar_t *p = loc;
						while (IsCharSpaceW(*p)) p = CharNextW(p);
						if (!*p) EndDialog(hwndDlg, 1);
						else recent_add(loc);
					}
				case IDCANCEL:
					GetWindowRect(hwndDlg, &loc_rect);
					EndDialog(hwndDlg, (LOWORD(wParam) == IDCANCEL));
					return FALSE;
				case IDRESET:
					if (LPMessageBox(hwndDlg, IDS_RESET_URLS, IDS_RESET_URLS_TITLE, MB_ICONQUESTION | MB_YESNO) == IDYES)
					{
						int x = 1;
						while(1)
						{
							char s[123] = {0};
							wchar_t name[FILENAME_SIZE] = {0};
							StringCchPrintfA(s, 123, "RecentURL%d", x);
							_r_sW(s, name, 123);
							_w_sW(s, 0);
							if (!name[0]) break;
							x++;
						}

						// clear out but keep what's been currently entered so as not to annoy people...
						GetDlgItemTextW(hwndDlg, IDC_URL_NEW, loc, ARRAYSIZE(loc));
						SendDlgItemMessage(hwndDlg, IDC_URL_NEW, CB_RESETCONTENT, 0, 0);
						SetDlgItemTextW(hwndDlg, IDC_URL_NEW, loc);
					}
					return FALSE;
			}
			return FALSE;
		case WM_CLOSE:
			EndDialog(hwndDlg, 2);
			return FALSE;
	}
	return 0;
}

LRESULT getNewLocation(int clearlist, HWND hwnd) // if clearlist is 1, playlist is cleared
{
	static int q;
	if (q) return 0;
	q = 1;
	if (!LPDialogBoxParamW(IDD_OPENLOC, hwnd, (DLGPROC)locProc, !clearlist))
	{
		wchar_t *beg = loc;
		wchar_t *l = loc;
		wchar_t buf[FILENAME_SIZE] = {0};
		wchar_t *pEnd;

		while (IsCharSpaceW(*l)) l = CharNextW(l);

		if (!wcsstr(l, L":/") && !wcsstr(l, L":\\"))
		{
			StringCchPrintfW(buf, FILENAME_SIZE, L"http://%s", l);
			l = buf;
			beg = buf;
		}

		if (clearlist < 0)
		{
			int psize = WideCharToMultiByte(CP_ACP, 0, l, -1, 0, 0, NULL, NULL);
			char* p = (char*)GlobalAlloc(GPTR, psize);
			WideCharToMultiByte(CP_ACP, 0, l, -1, p, psize, NULL, NULL);

			//trim any trailing spaces
			//l = p + lstrlen(p) - 1;
			if (IsCharSpaceW(*l))
			{
				while (IsCharSpaceW(*l))
					l = CharPrevW(beg, l);
				*CharNextW(l) = 0;
			}

			q = 0;
			return (LRESULT)p;
		}

		pEnd = GetLastCharacterW(l);
		if (IsCharSpaceW(*pEnd))
		{
			while (IsCharSpaceW(*pEnd))
				pEnd = CharPrevW(l, pEnd);
			*CharNextW(pEnd) = 0;
		}

		/* benski> CUT because 'idir' doesn't seem to be used
		int idir=0;
		HANDLE h;
		WIN32_FIND_DATA d;
		h =	FindFirstFile(l,&d);
		if (h!=INVALID_HANDLE_VALUE)
		{
		FindClose(h);
		if (d.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
		{
		idir=1;
		}
		}
		*/
		if (clearlist)
			PlayList_delete();
		PlayList_appendthing(l, 0, 0);
		if (clearlist)
			BeginPlayback();
	}
	q = 0;
	return 0;
}