musix 0.3.5

Music player library for esoteric audio formats (music from C64,Amiga etc)
Documentation
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
#include "stdafx.h"
#include "resource.h"

#include <string>
#include "PlayList.h"
#include "Psid.h"
#include "TedPlay.h"

#define STRING_EXCEPTION_EN _T("Exception occurred!")

bool CPlayList::EnumerateFolder(LPCTSTR lpcszFolder, LPTSTR ext, int nLevel)
{
	WIN32_FIND_DATA ffd;
	TCHAR szDirOnly[MAX_PATH];
	TCHAR szDir[MAX_PATH];
	HANDLE hFind = INVALID_HANDLE_VALUE;

	_tcscpy(szDirOnly, lpcszFolder);
	_tcscpy(szDir, lpcszFolder);
	_tcscat(szDir, ext);

	// Find the first file in the directory.
	hFind = FindFirstFile(szDir, &ffd);
	if (INVALID_HANDLE_VALUE == hFind) {
		return false;
	} 

	// List all the files in the directory with some info about them.
	_TCHAR szOutLine[MAX_PATH] = {0};
	for (int ii = 0; ii < nLevel; ++ii)
		szOutLine[ii] = _T('\t');

	do {
		if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
			if ( _T('.') != ffd.cFileName[0] ) {
				_tprintf(_T("%s%s   <DIR>\n"), szOutLine, ffd.cFileName);
				_tcscpy(szDir + _tcslen(lpcszFolder)+1, ffd.cFileName);
				EnumerateFolder(szDir, ext, nLevel + 1);    // Here's the recursive call!
			}
		} else {
			_TCHAR filePath[MAX_PATH];
			_tprintf(TEXT("%s%s\n"), szOutLine, ffd.cFileName);
			sprintf(filePath, _T("%s\\%s"), szDirOnly, ffd.cFileName);
			// add to playlist
			AddFileToPlaylist(filePath);
		}
	} while (FindNextFile(hFind, &ffd) != 0);

	FindClose(hFind);
	return true;
}

LRESULT CPlayList::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	// Init the CDialogResize code
	DlgResize_Init();
    //Bind keys...
    m_haccelerator = AtlLoadAccelerators(IDR_ACCELERATORS_PLAYLIST);
	
	// bind buttons with objects
	btnAdd.Attach(GetDlgItem(IDC_BTN_ADD));
	btnRemove.Attach(GetDlgItem(IDC_BTN_REMOVE));
	btnAddFolder.Attach(GetDlgItem(IDC_BTN_ADDFOLDER));
	btnLoadPlayList.Attach(GetDlgItem(IDC_BTN_LOADPL));
	btnSavePlayList.Attach(GetDlgItem(IDC_BTN_SAVEPL));
	btnPrevMod.Attach(GetDlgItem(IDC_BTN_PREVMODULE));
	btnPlayMod.Attach(GetDlgItem(IDC_BTN_PLAYSELECTION));
	btnNextMod.Attach(GetDlgItem(IDC_BTN_NEXTMODULE));
	HINSTANCE inst = _Module.GetResourceInstance();
	// load & set button icons
	HICON icon = (HICON) ::LoadImage(inst, MAKEINTRESOURCE(IDI_ICON_OPEN),
		IMAGE_ICON, 16, 16, LR_SHARED);
	btnLoadPlayList.SetIcon(icon);
	icon = (HICON) ::LoadImage(inst, MAKEINTRESOURCE(IDI_ICON_SAVE),
		IMAGE_ICON, 16, 16, LR_SHARED);
	btnSavePlayList.SetIcon(icon);
	icon = (HICON) ::LoadImage(inst, MAKEINTRESOURCE(IDI_ICON_PREV),
		IMAGE_ICON, 16, 16, LR_SHARED);
	btnPrevMod.SetIcon(icon);
	icon = (HICON) ::LoadImage(inst, MAKEINTRESOURCE(IDI_ICON_PLAY),
		IMAGE_ICON, 16, 16, LR_SHARED);
	btnPlayMod.SetIcon(icon);
	icon = (HICON) ::LoadImage(inst, MAKEINTRESOURCE(IDI_ICON_NEXT),
		IMAGE_ICON, 16, 16, LR_SHARED);
	btnNextMod.SetIcon(icon);
	//
	UINT uCToolTipCtrlStyle = TTS_NOPREFIX |TTS_NOFADE | TTS_NOANIMATE; // | TTS_BALLOON
	UINT uToolFlags = TTF_IDISHWND | TTF_SUBCLASS;

	ATLVERIFY(NULL != m_wndToolTip[0].Create(m_hWnd, 0, NULL, uCToolTipCtrlStyle)); // WS_POPUP|TTS_NOPREFIX|TTS_NOFADE|TTS_NOANIMATE
	CToolInfo toolInfo(uToolFlags, btnAdd.m_hWnd, 0, 0, _T("Add file(s)"));
	m_wndToolTip[0].AddTool(&toolInfo);
	m_wndToolTip[0].SetDelayTime(TTDT_INITIAL,0);
	m_wndToolTip[0].SetDelayTime(TTDT_RESHOW,0);
	m_wndToolTip[0].Activate(TRUE);
	
	ATLVERIFY(NULL != m_wndToolTip[1].Create(m_hWnd, 0, NULL, uCToolTipCtrlStyle));
	toolInfo.Init(uToolFlags, btnRemove.m_hWnd, 0, 0, _T("Remove File(s)"));
	m_wndToolTip[1].AddTool(&toolInfo);
	m_wndToolTip[1].Activate(TRUE);

	ATLVERIFY(NULL != m_wndToolTip[2].Create(m_hWnd, 0, NULL, uCToolTipCtrlStyle));
	toolInfo.Init(uToolFlags, btnAddFolder.m_hWnd, 0, 0, _T("Add Folder"));
	m_wndToolTip[2].AddTool(&toolInfo);
	m_wndToolTip[2].Activate(TRUE);
	
	// Create the listview columns
	playListView.Attach(GetDlgItem(IDC_LSV1));
	playListView.SetView(LV_VIEW_DETAILS);
	// FIXME doesnt work
	//playListView.SetExtendedListViewStyle(LVS_SHOWSELALWAYS);
	playListView.AddColumn(_T("Filename"), LV_FIELD_FILENAME);
	playListView.SetColumnWidth(LV_FIELD_FILENAME, 150);
	//
	playListView.AddColumn(_T("Title"), LV_FIELD_TITLE);
	playListView.SetColumnWidth(LV_FIELD_TITLE, 120);
	//
	playListView.AddColumn(_T("Author"), LV_FIELD_AUTHOR);
	playListView.SetColumnWidth(LV_FIELD_TITLE, 250);
	//
	playListView.AddColumn(_T("Released"), LV_FIELD_RELEASED);
	playListView.SetColumnWidth(LV_FIELD_TITLE, 200);
	//
	playListView.AddColumn(_T("Path"), LV_FIELD_PATH);
	playListView.SetColumnWidth(LV_FIELD_PATH, 300);
	//
	playListView.AddColumn(_T("Status"), LV_FIELD_STATUS);
	playListView.SetColumnWidth(LV_FIELD_STATUS, 50);
	//
	playListView.AddColumn(_T("Type"), LV_FIELD_TYPE);
	playListView.SetColumnWidth(LV_FIELD_TYPE, 50);
	//
	playListView.AddColumn(_T("Load address"), LV_FIELD_LOAD_ADDRESS);
	playListView.SetColumnWidth(LV_FIELD_LOAD_ADDRESS, 32);
	//
	playListView.AddColumn(_T("#"), LV_FIELD_INDEX);
	playListView.SetColumnWidth(LV_FIELD_INDEX, 32);

	// register hotkeys for the playlist
	::RegisterHotKey(m_hWnd, 1, MOD_ALT | MOD_CONTROL, VK_RIGHT);
	::RegisterHotKey(m_hWnd, 2, MOD_ALT | MOD_CONTROL, VK_LEFT);
	//
	// register object for message filtering and idle updates    
    CMessageLoop* pLoop = _Module.GetMessageLoop();
    ATLASSERT(pLoop != NULL);
    pLoop->AddMessageFilter(this);
    pLoop->AddIdleHandler(this);
	return TRUE;
}

LRESULT CPlayList::OnDestroy(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
{
	HWND parent = GetParent();
	HMENU hmenu = ::GetMenu(m_hwndParent);
	ShowWindow(SW_HIDE);
	CheckMenuItem(hmenu, IDM_VIEW_PLAYLIST, MF_UNCHECKED);
	::UnregisterHotKey(m_hWnd, 1);
	::UnregisterHotKey(m_hWnd, 2);
	return 0;
}

BOOL CPlayList::AddFileToPlaylist(_TCHAR *fullPath)
{
	_TCHAR tmp[MAX_PATH];

	_tcscpy(tmp, fullPath);
	PathStripPath(tmp);
	PathRemoveExtension(tmp);
	UINT index = playListView.GetItemCount();
	//
	_TCHAR indexText[16];
	_stprintf(indexText, _T("%i"), index + 1);
	playListView.AddItem(index, LV_FIELD_FILENAME, tmp);
	playListView.AddItem(index, LV_FIELD_PATH, fullPath);
	playListView.AddItem(index, LV_FIELD_STATUS, _T("OK"));
	FILE *fp = _tfopen(fullPath, _T("rb"));
	if (fp) {
		PsidHeader hdr;
		tedPlayGetInfo(fp, hdr);
		playListView.AddItem(index, LV_FIELD_TYPE, hdr.typeName.c_str());
		playListView.AddItem(index, LV_FIELD_TITLE, hdr.title);
		playListView.AddItem(index, LV_FIELD_AUTHOR, hdr.author);
		playListView.AddItem(index, LV_FIELD_RELEASED, hdr.copyright);
		_stprintf(tmp, _T("$%04X"), hdr.loadAddress);
		playListView.AddItem(index, LV_FIELD_LOAD_ADDRESS, tmp);
		fclose(fp);
	}
	playListView.AddItem(index, LV_FIELD_INDEX, indexText);
	return TRUE;
}

LRESULT CPlayList::OnDropFiles(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& bHandled)
{
	HDROP hDrop = (HDROP) wParam;
	_TCHAR namebuffer[MAX_PATH];

	UINT count = ::DragQueryFile(hDrop, -1, namebuffer, MAX_PATH);
	while (count--) {
		DragQueryFile(hDrop, count, namebuffer, MAX_PATH);
		AddFileToPlaylist(namebuffer);
	} 
	::DragFinish(hDrop);
	::SetForegroundWindow(m_hWnd);
	return 0;
}

LRESULT CPlayList::OnGetMinMaxInfo(UINT /*uMsg*/, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	return 0L;
}

LRESULT CPlayList::OnResizing(UINT /*uMsg*/, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	return 0L;
}

LRESULT CPlayList::OnHotkey(UINT /*uMsg*/, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	switch (wParam) {
		case 1:
			stepPlayList(+1);
			OnNMDblclkLsv(0, 0, bHandled);
			::PostMessage(m_hwndParent, WM_USER + 2, 0, 0);
			break;
		case 2:
			stepPlayList(-1);
			OnNMDblclkLsv(0, 0, bHandled);
			::PostMessage(m_hwndParent, WM_USER + 2, 0, 0);
			break;
		default:
			;
	}
	return 0L;
}

LRESULT CPlayList::OnLvReturn(int /*idCtrl*/, LPNMHDR pNMHDR, BOOL& bHandled)
{
	OnNMDblclkLsv(0, 0, bHandled);
	return 0L;
}

int CPlayList::savePlaylist(_TCHAR * plName)
{
	FILE *fp = _tfopen(plName, _T("wt"));
	if (!fp)
		return 1;
	try {
		for(int i = 0; i < playListView.GetItemCount(); i++) {
			_TCHAR name[MAX_PATH];
			if (playListView.GetItemText(i, LV_FIELD_PATH, name, MAX_PATH)) {
				_fputts(name, fp);
				_ftprintf(fp, _T("\n"));
			}
		}
	} catch(_TCHAR *str) {
		MessageBox(str, STRING_EXCEPTION_EN, MB_ICONERROR);
	}
	fclose(fp);
	return 0;
}

int CPlayList::loadPlaylist(_TCHAR * plName)
{
	FILE *fp = _tfopen(plName, _T("rt"));
	if (!fp)
		return 1;
	int index = 0;
	try {
		while (!feof(fp)) {
			_TCHAR line[MAX_PATH] = { 0 };
			_TCHAR name[MAX_PATH] = { 0 };
			_fgetts(line, MAX_PATH, fp);
			int s = _stscanf(line, "%[^,\n,\r]", name);
			if (s == 1 && _tcslen(name)) {
				AddFileToPlaylist(name);
				BOOL status = ::PathFileExists(name);
				playListView.AddItem(index, LV_FIELD_STATUS, status ? _T("OK") : _T("Error!"));
			}
			index++;
		}
	} catch(_TCHAR *str) {
		MessageBox(str, STRING_EXCEPTION_EN, MB_ICONERROR);
	}
	fclose(fp);
	return 0;
}

static int CALLBACK StrCompareFunc(LPARAM lParam1, LPARAM lParam2,
								   LPARAM lParamSort)
{
	// First two parameters are item data to be compared.
	// Third parameter is bSortDescending
	CString* pStr1 = (CString*)lParam1;
	CString* pStr2 = (CString*)lParam2;

	if (*pStr1 < *pStr2) return lParamSort ? 1 : -1;
	if (*pStr1 > *pStr2) return lParamSort ? -1 : 1;
	return 0;
}

BOOL CPlayList::SortList(int iColumn, bool bDescending)
{
	ATLASSERT((iColumn >= 0) && (iColumn < GetColumnCount()));
//	CWaitCursor waitcursor;
	int iItem;
	const int nLen = MAX_PATH;
	LVITEM lviGet;
	memset(&lviGet, 0, sizeof(LVITEM));
	lviGet.cchTextMax = nLen;
	lviGet.iSubItem = iColumn;
	LVITEM lviSet = {LVIF_PARAM};

	// ... case Sort as text
	for (iItem = 0; iItem < playListView.GetItemCount() ; iItem++) {
		CString* pStr = new CString;
		lviGet.pszText = pStr->GetBufferSetLength(nLen);
		playListView.SendMessage(LVM_GETITEMTEXT, (WPARAM)iItem, (LPARAM)&lviGet);
		pStr->ReleaseBuffer();
		lviSet.iItem = iItem;
		lviSet.lParam = (LPARAM)pStr;
		playListView.SetItem( &lviSet );
	}
	// here is the call to LVM_SORTITEMS
	playListView.SortItems((PFNLVCOMPARE) &StrCompareFunc, (LPARAM)bDescending);

	// cleanup
	for (iItem = 0; iItem < playListView.GetItemCount() ; iItem++) {
		lviSet.iItem = iItem;
		playListView.GetItem(&lviSet);
		if (lviSet.lParam)
			delete (CString*)(lviSet.lParam);
	}
	// ... end Sort as text
	// Sort as integer, double, or other type
	reIndex(0, 0);

//	UpdateSortIcons(iColumn, bDescending);
	return TRUE;
}

void CPlayList::reIndex(int fromCol, int toCol)
{
	int items = playListView.GetItemCount();

	for(int i = 0; i < playListView.GetItemCount(); i++) {
		_TCHAR indexText[16];
		_stprintf(indexText, _T("%i"), i + 1);
		playListView.SetItem(i, LV_FIELD_INDEX, LVIF_TEXT, indexText, 0, 0, 0, 0);
	}
}

LRESULT CPlayList::OnLvnColumnclickLsv(int /*idCtrl*/, LPNMHDR pNMHDR, BOOL& /*bHandled*/)
{
	LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);

	// no sorting for index column
	if (pNMLV->iSubItem == LV_FIELD_INDEX)
		return 0;
	// if the same columns is clicked, sort descending
	m_bSortDescending = (m_iSortColumn == pNMLV->iSubItem) ? !m_bSortDescending : false;
	SortList(pNMLV->iSubItem, m_bSortDescending);
	// remember which column was sorted last
	m_iSortColumn = pNMLV->iSubItem;
	return 0;
}

LRESULT CPlayList::OnNMDblclkLsv(int idCtrl, LPNMHDR pNMHDR, BOOL& /*bHandled*/)
{
	UINT index = playListView.GetSelectionMark();
	_TCHAR namebuffer[MAX_PATH];

	if (playListView.GetItemText(index, LV_FIELD_PATH, namebuffer, MAX_PATH)) {
		if (::PathFileExists(namebuffer) && !tedplayMain(namebuffer, NULL)) {
			// update info
			::PostMessage(m_hwndParent, WM_USER + 1, 0, 0);
			// reset autoskip timer
			::PostMessage(m_hwndParent, WM_USER + 2, 0, 0);
			playListView.AddItem(index, LV_FIELD_STATUS, _T("OK"));
		} else {
			playListView.AddItem(index, LV_FIELD_STATUS, _T("Error!"));
		}
	}
	return 0;
}

LRESULT CPlayList::OnPlayTune(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& bHandled)
{
	OnNMDblclkLsv(0, 0, bHandled);
	return 0;
}

LRESULT CPlayList::OnBnClickedBtnAdd(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	const int FILENAMES_BUFFER_SIZE = 32048;
	static unsigned int selFilter = 0;
	_TCHAR szFileNamesBuffer[FILENAMES_BUFFER_SIZE] = {0};
	_TCHAR szFilter[] = _T("TED tunes (*.c8m;*.prg)\0"
											"*.c8m;*.prg\0"
											"SID tunes (*.sid)\0*.sid\0"
								"All Files (*.*)\0*.*\0\0");
	WTL::CFileDialog wndFileDialog ( TRUE, NULL, NULL, 
		OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_EXPLORER | OFN_ALLOWMULTISELECT, 
		szFilter, m_hWnd );
	wndFileDialog.m_ofn.lpstrFile = szFileNamesBuffer;
	wndFileDialog.m_ofn.nMaxFile = FILENAMES_BUFFER_SIZE;

	wndFileDialog.m_ofn.nFilterIndex = selFilter;
	if (IDOK == wndFileDialog.DoModal() ) {
		_TCHAR sDirectory[MAX_PATH];
		_TCHAR sFullPath[MAX_PATH];

		::GetCurrentDirectory(MAX_PATH, sDirectory);
		selFilter = wndFileDialog.m_ofn.nFilterIndex;
		//PathStripToRoot(sDirectory);
		LPTSTR sFileName = wndFileDialog.m_ofn.lpstrFile + wndFileDialog.m_ofn.nFileOffset;

		while(sFileName != NULL && *sFileName != _T('\0')) {
			sFullPath[0] = _T('\0');
			::PathCombine(sFullPath, sDirectory, sFileName);
			AddFileToPlaylist(sFullPath);
			// path is the file
			sFileName += _tcslen(sFileName) + 1;
		}
	}
	return 0;
}

LRESULT CPlayList::OnBnClickedBtnRemove(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	 int nSelRows = playListView.GetSelectedCount();
	 if (!nSelRows)
		 return 1;
	 int *pnArrayOfSelRows = new int[nSelRows];
	 if (!pnArrayOfSelRows)
		 return 2;
	 int nIndex = -1;
	 // Loop through all selected items
	 for (int i = 0; i < nSelRows; i++) {
		 // Search for the next selected list control item and get its index
		 nIndex = playListView.GetNextItem(nIndex, LVNI_SELECTED);
		 if (nIndex != LB_ERR)
			pnArrayOfSelRows[i] = nIndex;
	 }
	 for(int j = nSelRows - 1; j >= 0; j--) {
		 playListView.DeleteItem(pnArrayOfSelRows[j]);
	 }
	 // select the proper item
	 int index = playListView.GetItemCount();
	 if (index) {
		 int newsel;
		 if (index != pnArrayOfSelRows[0])
			 newsel = pnArrayOfSelRows[0];
		 else
			 newsel = index - 1;
		 playListView.SetFocus();
		 playListView.SetItemState(newsel, LVIS_SELECTED, LVIS_SELECTED);
	 }
	 delete(pnArrayOfSelRows);
	 pnArrayOfSelRows = NULL;

	 reIndex(0, 0);

	 return 0;
}

LRESULT CPlayList::OnLvnKeydownLsv1(int /*idCtrl*/, LPNMHDR pNMHDR, BOOL& bHandled)
{
	LPNMLVKEYDOWN pLVKeyDow = reinterpret_cast<LPNMLVKEYDOWN>(pNMHDR);
	// TODO: Add your control notification handler code here
	if (pLVKeyDow->wVKey == VK_DELETE) {
		OnBnClickedBtnRemove(0, 0, m_hWnd, bHandled);
	} else if (pLVKeyDow->wVKey == VK_ADD) {
		OnSelectAll(0, 0, 0, bHandled);
	} else if (pLVKeyDow->wVKey == VK_SPACE) {
		OnNMDblclkLsv(0, 0, bHandled);
	}
	return 0;
}

int CPlayList::stepPlayList(int direction)
{
	int items = playListView.GetItemCount();
	if (!items)
		return 1;
	int sel = playListView.GetSelectionMark();
	playListView.SetItemState(-1, 0, LVIS_SELECTED | LVIS_FOCUSED);
	if (sel + direction < 0) {
		playListView.SetItemState(items - 1, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
		playListView.SetSelectionMark(items - 1);
	} else if (sel + direction >= items) {
		playListView.SetItemState(0, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
		playListView.SetSelectionMark(0);
	} else {
		playListView.SetItemState(sel + direction, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
		playListView.SetSelectionMark(sel + direction);
	}
	return 0;
}

LRESULT CPlayList::OnMouseMessage(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	MSG msg = {m_hWnd, uMsg, wParam, lParam};

	for(unsigned int i = 0; i < 3; i++) {
		if (m_wndToolTip[i].IsWindow()) {
			m_wndToolTip[0].RelayEvent(&msg);
		}
	}
	bHandled = FALSE;
	return 1;
}

LRESULT CPlayList::OnBnClickedBtnAddfolder(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	static _TCHAR strFolderPath[MAX_PATH] = { 0 };
	// TODO: Add your control notification handler code here
	CFolderDialog dlgFolder(m_hWnd, _T("Add files recoursively from folder..."),
		BIF_NEWDIALOGSTYLE | BIF_RETURNONLYFSDIRS);

	if (!::PathIsDirectory(strFolderPath))
		::GetCurrentDirectory(MAX_PATH, strFolderPath);
	dlgFolder.SetInitialFolder(strFolderPath);
	if ( IDOK == dlgFolder.DoModal()) {
		_tcscpy(strFolderPath, dlgFolder.GetFolderPath());
		//EnumerateFolder(strFolderPath, _T("\\*.prg"));
		//EnumerateFolder(strFolderPath, _T("\\*.c8m"));
		EnumerateFolder(strFolderPath, _T("\\*"));
	}
	return 0;
}

LRESULT CPlayList::OnSelectAll(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	playListView.SetFocus();
	int index = playListView.GetItemCount();
	while (index) {
		index--;
		playListView.SetItemState(index, LVIS_SELECTED, LVIS_SELECTED);
	}
	return 0;
}

LRESULT CPlayList::OnNMRclickLsv1(int /*idCtrl*/, LPNMHDR pNMHDR, BOOL& /*bHandled*/)
{
	OnIdle(); //force UI Updating
	POINT pt;
	GetCursorPos(&pt);

	CMenu menu;
	menu.LoadMenu(IDR_POPUPMENU_PLIST);
	CMenuHandle menuPop = menu.GetSubMenu(0);
	
    // Display the shortcut menu. Track the right mouse button. 
	::TrackPopupMenuEx(menuPop, 
            TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTBUTTON, 
            pt.x, pt.y, m_hWnd, NULL); 

	return 0;
}

LRESULT CPlayList::OnRootOpenfilelocation(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	std::string path;
	UINT index = playListView.GetSelectionMark();
	_TCHAR namebuffer[MAX_PATH] = _T("");

	if (playListView.GetItemText(index, LV_FIELD_PATH, namebuffer, MAX_PATH)) {
		_TCHAR *end = _tcsrchr(namebuffer, _T('\\'));
		if (end) {
			*end = _T('\0');
		} else {
			MessageBox(_T("Could not open file location."), _T("Error!"), MB_ICONERROR);
			return 1;
		}
	}
	HINSTANCE hinst = ::ShellExecute(m_hWnd, _T("open"), _T("explorer.exe"), namebuffer, NULL, SW_SHOWNORMAL);
	return 0;
}

LRESULT CPlayList::OnBnClickedBtnLoadpl(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	unsigned int selFilter = 0;
	_TCHAR szFilter[] = _T("Tedplay playlists (*.pls)\0"
											"*.pls\0"
								"All Files (*.*)\0*.*\0\0");
	WTL::CFileDialog wndFileDialog ( TRUE, NULL, NULL, 
		OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_EXPLORER, 
		szFilter, m_hWnd );

	wndFileDialog.m_ofn.nFilterIndex = selFilter;
	if (IDOK == wndFileDialog.DoModal() ) {
		LPTSTR sFileName = wndFileDialog.m_ofn.lpstrFile;
		selFilter = wndFileDialog.m_ofn.nFilterIndex;
		playListView.DeleteAllItems();
		loadPlaylist(sFileName);
	}
	return 0;
}

LRESULT CPlayList::OnBnClickedBtnSavepl(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	unsigned int selFilter = 0;
	_TCHAR szFilter[] = _T("Tedplay playlists (*.pls)\0"
											"*.pls\0"
								"All Files (*.*)\0*.*\0\0");
	WTL::CFileDialog wndFileDialog(FALSE, NULL, NULL, 
		OFN_PATHMUSTEXIST | OFN_EXPLORER, szFilter, m_hWnd);

	wndFileDialog.m_ofn.nFilterIndex = selFilter;
	if (IDOK == wndFileDialog.DoModal() ) {
		LPTSTR sFileName = wndFileDialog.m_ofn.lpstrFile;
		selFilter = wndFileDialog.m_ofn.nFilterIndex;
		savePlaylist(sFileName);
	}
	return 0;
}

LRESULT CPlayList::OnBnClickedBtnNextModule(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& bHandled)
{
	// step forward one module in the playlist
	OnHotkey(0, 1, 0, bHandled);
	return 0;
}

LRESULT CPlayList::OnBnClickedBtnPlayselection(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& bHandled)
{
	// play the current selection
	OnNMDblclkLsv(0, 0, bHandled);
	return 0;
}

LRESULT CPlayList::OnBnClickedBtnPrevmodule(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& bHandled)
{
	// step back one module in the playlist
	OnHotkey(0, 2, 0, bHandled);
	return 0;
}