diff --git a/ClickPlay.rc b/ClickPlay.rc index ed5baa2..3f1cd81 100644 Binary files a/ClickPlay.rc and b/ClickPlay.rc differ diff --git a/Resource.h b/Resource.h index 810710b..b8fee40 100644 --- a/Resource.h +++ b/Resource.h @@ -12,13 +12,12 @@ #define IDC_CLICKPLAY 109 #define IDR_MAINFRAME 128 #define IDC_MENU 200 -#define ID_PLIK_OD32774 32774 #define ID_REFRESH 32775 #define ID_OPCJE_POWTARZAJ 32777 #define ID_OPCJE_ONCLICK 32778 #define ID_OPCJE_LOOP 32779 -#define ID_POMOC_SKR32782 32782 #define ID_POMOC_KBSHR 32783 +#define ID_PLIK_CLOSEDIR 32786 #define IDC_STATIC -1 // Next default values for new objects @@ -27,7 +26,7 @@ #ifndef APSTUDIO_READONLY_SYMBOLS #define _APS_NO_MFC 1 #define _APS_NEXT_RESOURCE_VALUE 130 -#define _APS_NEXT_COMMAND_VALUE 32785 +#define _APS_NEXT_COMMAND_VALUE 32789 #define _APS_NEXT_CONTROL_VALUE 1000 #define _APS_NEXT_SYMED_VALUE 110 #endif diff --git a/main.cpp b/main.cpp index be5b028..8a49fc6 100644 --- a/main.cpp +++ b/main.cpp @@ -24,6 +24,7 @@ using std::wstring; #define NOT_PLAYING_LABEL L"Nic nie jest odtwarzane" #define PLAY_LABEL L"Odtwarzaj" #define PAUSE_LABEL L"Pauza" +#define NO_DIR_LOADED_LABEL L"Nie wybrano folderu" //GLOBAL VARS HFONT hFont = CreateFont(18, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, TEXT("Segoe UI")); @@ -37,14 +38,15 @@ wstring selected; //STATE VARS /* -* -1 - not loaded -* 0 - nothing is playing +* -1 - not ready +* 0 - ready * 1 - playback active * 2 - playback paused */ int currentState = -1; BOOL isAutoPlayEnabled = TRUE; BOOL isLoopEnabled = FALSE; +BOOL isQueued = FALSE; //CONTROLS HWND mainWindow; @@ -104,7 +106,7 @@ int WINAPI WinMain(HINSTANCE currentInst, HINSTANCE previousInst, LPSTR lpCmdLin //CREATE CONTROLS void CreateControls(HWND window) { - dirLabel = CreateWindowEx(0, L"STATIC", L"Nie wybrano folderu", WS_CHILD | WS_VISIBLE, 10, 10, 250, 40, window, (HMENU)IDC_DIRLABEL, globalInstance, NULL); + dirLabel = CreateWindowEx(0, L"STATIC", NO_DIR_LOADED_LABEL, WS_CHILD | WS_VISIBLE, 10, 10, 250, 40, window, (HMENU)IDC_DIRLABEL, globalInstance, NULL); SendMessage(dirLabel, WM_SETFONT, (WPARAM)hFont, TRUE); browseButton = CreateWindowEx(0, L"BUTTON", L"Przeglądaj", WS_CHILD | WS_VISIBLE, 275, 10, 100, 30, window, (HMENU)IDC_BROWSEBTN, globalInstance, NULL); SendMessage(browseButton, WM_SETFONT, (WPARAM)hFont, TRUE); @@ -118,21 +120,108 @@ void CreateControls(HWND window) { SendMessage(stopButton, WM_SETFONT, (WPARAM)hFont, TRUE); } -//GET FILES FROM DIRECTORY -void GetFiles(wstring path, wstring ext) { - if (path[0] != NULL) { - wstring findPathS = path + L"\\*." + ext; - LPCWSTR findPath = findPathS.c_str(); - WIN32_FIND_DATA data; - HANDLE hFind = FindFirstFile(findPath, &data); - - if (hFind != INVALID_HANDLE_VALUE) { - do { - SendMessage(filesListCtrl, LB_ADDSTRING, NULL, (LPARAM)data.cFileName); - } while (FindNextFile(hFind, &data)); - EnableWindow(filesListCtrl, TRUE); +//GET SELECTED INDEX +int GetIndex() { + LRESULT listItemsCount = SendMessage(filesListCtrl, LB_GETCOUNT, NULL, NULL); + for (int i = 0; i < listItemsCount; i++) { + if (SendMessage(filesListCtrl, LB_GETSEL, (WPARAM)i, NULL) > 0) { + return i; } } + return -1; +} + +//REMOVE SELECTION +void UnloadSelection() { + mciSendString(L"close mp3", NULL, 0, mainWindow); + selected.clear(); +} + +//LOAD SELECTION +/* +* ERROR CODES +* -1 - invalid index +* -2 - file not found +* -3 - path not loaded +*/ +int LoadSelection(int index) { + UnloadSelection(); + if (index != -1) { + LRESULT selectedLength = SendMessage(filesListCtrl, LB_GETTEXTLEN, (WPARAM)index, NULL); + TCHAR* selectionBuffer = new TCHAR[selectedLength]; + SendMessage(filesListCtrl, LB_GETTEXT, (WPARAM)index, (LPARAM)selectionBuffer); + if (!path.empty()) { + wstring fullPath = path + L"\\" + selectionBuffer; + WIN32_FIND_DATA fileData; + HANDLE fileExist = FindFirstFile(fullPath.c_str(), &fileData); + if (fileExist == INVALID_HANDLE_VALUE) { + MessageBox(mainWindow, L"Wskazany plik nie został odnaleziony. Upewnij się, że nie został przeniesiony lub naciśnij klawisz F5, aby pozbyć się niedziałających pozycji.", L"Nie znaleziono pliku", MB_ICONERROR); + return -2; + } + else { + wstring fullCommandS = L"open \"" + fullPath + L"\" type mpegvideo alias mp3"; + LPCWSTR fullCommand = fullCommandS.c_str(); + + mciSendString(fullCommand, NULL, 0, mainWindow); + selected = wstring(selectionBuffer); + return 0; + } + } + else { + MessageBox(mainWindow, L"Naruszenie dostępu do stanu programu. Proces zostanie zakończony.", L"Błąd krytyczny", MB_ICONERROR); + ExitProcess(0); + return -3; + } + } + else { + return -1; + } +} + +//UNLOAD DIRECTORY +void UnloadDir() { + SendMessage(filesListCtrl, LB_RESETCONTENT, NULL, NULL); + path.clear(); + if (!selected.empty()) { + UnloadSelection(); + } +} + +//LOAD DIRECTORY +/* +* ERROR CODES +* -1 - empty string +* -2 - invalid directory +*/ +int LoadDir(wstring dir) { + UnloadDir(); + if (!dir.empty()) { + DWORD dirA = GetFileAttributes(dir.c_str()); + if (dirA & FILE_ATTRIBUTE_DIRECTORY) { + SendMessage(filesListCtrl, LB_RESETCONTENT, NULL, NULL); + for (int i = 0; i < EXT_LENGTH; i++) { + wstring findPathS = dir + L"\\*." + ext[i]; + LPCWSTR findPath = findPathS.c_str(); + WIN32_FIND_DATA dirData; + HANDLE finder = FindFirstFile(findPath, &dirData); + if (finder != INVALID_HANDLE_VALUE) { + do { + SendMessage(filesListCtrl, LB_ADDSTRING, NULL, (LPARAM)dirData.cFileName); + } while (FindNextFile(finder, &dirData)); + } + } + path = dir; + return 0; + } + else { + MessageBox(mainWindow, L"Program nie może uzyskać dostępu do wskazanego katalogu.", L"Nieprawidłowy katalog", MB_ICONERROR); + return -2; + } + } + else { + MessageBox(mainWindow, L"Nie załadowano folderu. Użyj przycisku PRZEGLĄDAJ, aby załadaować katalog.", L"Brak katalogu", MB_ICONERROR); + return -1; + } } //INITALIZE FOLDER PICKER @@ -145,52 +234,20 @@ BOOL FolderBrowseDialog(HWND window) { TCHAR currentPath[MAX_PATH]; if (pid != 0) { SHGetPathFromIDList(pid, currentPath); - path = wstring(currentPath); - SetCurrentDirectory(path.c_str()); - SetWindowText(dirLabel, path.c_str()); - return TRUE; + if (LoadDir((wstring)currentPath) == 0) { + SetCurrentDirectory(path.c_str()); + SetWindowText(dirLabel, path.c_str()); + return TRUE; + } + else { + return FALSE; + } } else { return FALSE; } } -//MCI PROCESSOR -void Play(wstring path, wstring fileName) { - if (path[0] != NULL && selected.empty() == false) { - //STOP PREVIOUS PLAYBACKS - mciSendString(L"close mp3", NULL, 0, NULL); - - //GET PATH - wstring fullPath = path + L"\\" + selected; - WIN32_FIND_DATA fileData; - HANDLE fileExist = FindFirstFile(fullPath.c_str(), &fileData); - if (fileExist == INVALID_HANDLE_VALUE) { - MessageBox(mainWindow, L"Podany plik nie został odnaleziony. Upewnij się, że plik istnieje lub nie został przeniesiony.", L"Nie znaleziono pliku", MB_ICONERROR); - SendMessage(mainWindow, STATE_CHANGE, 0, NULL); - return; - } - - //PROCESS COMMAND - wstring fullCommandS = L"open \"" + fullPath + L"\" type mpegvideo alias mp3"; - LPCWSTR fullCommand = fullCommandS.c_str(); - - //PLAY THE SOUND - mciSendString(fullCommand, NULL, 0, NULL); - if (isLoopEnabled) { - mciSendString(L"play mp3 repeat", NULL, 0, mainWindow); - } - else { - mciSendString(L"play mp3 notify", NULL, 0, mainWindow); - } - } - else { - MessageBox(mainWindow, L"Podczas odtwarzania wystąpił błąd. Sprawdź czy podany plik istnieje.", L"Wystąpił błąd", MB_ICONERROR); - SendMessage(mainWindow, STATE_CHANGE, 0, NULL); - return; - } -} - //MESSAGE LOOP LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { int windowId, windowEvent; @@ -211,58 +268,40 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (windowId) { case IDC_BROWSEBTN: if (FolderBrowseDialog(mainWindow) == TRUE) { - mciSendString(L"close mp3", NULL, 0, NULL); SendMessage(mainWindow, STATE_CHANGE, 0, NULL); - SendMessage(filesListCtrl, LB_RESETCONTENT, NULL, NULL); - for (int i = 0; i < EXT_LENGTH; i++) { - GetFiles(path, ext[i]); - } } break; case IDC_LIST: if (windowEvent == LBN_SELCHANGE) { - LRESULT listItemsCount = SendMessage(filesListCtrl, LB_GETCOUNT, NULL, NULL); - int selectedItemIndex = -1; - for (int i = 0; i < listItemsCount; i++) { - if (SendMessage(filesListCtrl, LB_GETSEL, (WPARAM)i, NULL) > 0) { - selectedItemIndex = i; - break; + if (currentState == 1 || currentState == 2) { currentState = 1; isQueued = TRUE; } + if (LoadSelection(GetIndex()) == 0) { + if (isAutoPlayEnabled == TRUE) { + SendMessage(mainWindow, STATE_CHANGE, 1, NULL); } } - if (selectedItemIndex != -1) { - LRESULT selectedLength = SendMessage(filesListCtrl, LB_GETTEXTLEN, (WPARAM)selectedItemIndex, NULL); - TCHAR* selectionBuffer = new TCHAR[selectedLength]; - SendMessage(filesListCtrl, LB_GETTEXT, (WPARAM)selectedItemIndex, (LPARAM)selectionBuffer); - selected = wstring(selectionBuffer); - } - if (isAutoPlayEnabled == TRUE) { - Play(path, selected); - SendMessage(mainWindow, STATE_CHANGE, 1, NULL); - } - } - if (windowEvent == LBN_SELCANCEL) { - selected.clear(); } break; case IDC_PLAYBTN: switch (currentState) { case 0: - SendMessage(mainWindow, STATE_CHANGE, 1, NULL); - Play(path, selected); + if (LoadSelection(GetIndex()) == 0) { + SendMessage(mainWindow, STATE_CHANGE, 1, NULL); + } + else { + MessageBox(mainWindow, L"Podczas odtwarzania wystąpił błąd", L"Błąd odtwarzacza", MB_ICONERROR); + } break; case 1: SendMessage(mainWindow, STATE_CHANGE, 2, NULL); - mciSendString(L"pause mp3", NULL, 0, NULL); break; case 2: SendMessage(mainWindow, STATE_CHANGE, 1, NULL); - mciSendString(L"resume mp3", NULL, 0, NULL); break; } break; case IDC_STOPBTN: - mciSendString(L"close mp3", NULL, 0, NULL); - SendMessage(mainWindow, STATE_CHANGE, 0, NULL); + isQueued = FALSE; + SendMessage(mainWindow, MM_MCINOTIFY, 0, 0); break; case IDM_EXIT: SendMessage(mainWindow, WM_CLOSE, NULL, NULL); @@ -271,15 +310,7 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { DialogBox(globalInstance, MAKEINTRESOURCE(IDD_ABOUTBOX), mainWindow, About); break; case ID_REFRESH: - if (path[0] != NULL) { - SendMessage(filesListCtrl, LB_RESETCONTENT, NULL, NULL); - for (int i = 0; i < EXT_LENGTH; i++) { - GetFiles(path, ext[i]); - } - } - else { - MessageBox(mainWindow, L"Nie załadowano folderu. Naciśnij przycisk PRZEGLĄDAJ, aby wczytać katalog.", L"Brak katalogu", MB_ICONERROR); - } + LoadDir(path); break; case ID_OPCJE_ONCLICK: if (isAutoPlayEnabled) { @@ -304,44 +335,81 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { case ID_POMOC_KBSHR: MessageBox(mainWindow, L"F1 = ekran skrótów klawiszowych\nF2 = ekran Informacje\nF5 = odśwież listę plików", L"Skróty klawiszowe", MB_ICONASTERISK); break; + case ID_PLIK_CLOSEDIR: + SendMessage(mainWindow, STATE_CHANGE, -1, NULL); + break; } break; case MM_MCINOTIFY: - mciSendString(L"close mp3", NULL, 0, NULL); - SendMessage(mainWindow, STATE_CHANGE, 0, NULL); + if (!isQueued) { + UnloadSelection(); + SendMessage(mainWindow, STATE_CHANGE, 0, NULL); + } + else { + isQueued = FALSE; + } break; case STATE_CHANGE: - currentState = (int)wParam; - switch (currentState) { + switch ((int)wParam) { case -1: + UnloadSelection(); + UnloadDir(); + EnableWindow(filesListCtrl, FALSE); EnableWindow(playButton, FALSE); EnableWindow(stopButton, FALSE); EnableMenuItem(menuBar, ID_REFRESH, MF_DISABLED); + SetWindowText(dirLabel, NO_DIR_LOADED_LABEL); SetWindowText(npLabel, L"-"); SetWindowText(playButton, PLAY_LABEL); break; case 0: - EnableWindow(filesListCtrl, TRUE); - EnableWindow(playButton, TRUE); - EnableWindow(stopButton, TRUE); - EnableMenuItem(menuBar, ID_REFRESH, MF_ENABLED); - SetWindowText(npLabel, NOT_PLAYING_LABEL); - SetWindowText(playButton, PLAY_LABEL); - break; + if (!path.empty()) { + EnableWindow(filesListCtrl, TRUE); + EnableWindow(playButton, TRUE); + EnableWindow(stopButton, TRUE); + EnableMenuItem(menuBar, ID_REFRESH, MF_ENABLED); + SetWindowText(npLabel, NOT_PLAYING_LABEL); + SetWindowText(playButton, PLAY_LABEL); + break; + } + else { + SendMessage(mainWindow, STATE_CHANGE, -1, 0); + break; + } case 1: - EnableWindow(filesListCtrl, FALSE); - EnableWindow(playButton, TRUE); - EnableWindow(stopButton, TRUE); - EnableMenuItem(menuBar, ID_REFRESH, MF_DISABLED); - SetWindowText(npLabel, selected.c_str()); - SetWindowText(playButton, PAUSE_LABEL); - break; + if (!path.empty() && !selected.empty()) { + if (currentState == 2) { + mciSendString(L"resume mp3", NULL, 0, mainWindow); + } + else { + if (isLoopEnabled) { + mciSendString(L"play mp3 repeat", NULL, 0, mainWindow); + } + else { + mciSendString(L"play mp3 notify", NULL, 0, mainWindow); + } + } + + EnableWindow(filesListCtrl, TRUE); + EnableWindow(playButton, TRUE); + EnableWindow(stopButton, TRUE); + EnableMenuItem(menuBar, ID_REFRESH, MF_DISABLED); + SetWindowText(npLabel, selected.c_str()); + SetWindowText(playButton, PAUSE_LABEL); + break; + } + else { + SendMessage(mainWindow, STATE_CHANGE, 0, NULL); + break; + } case 2: + mciSendString(L"pause mp3", NULL, 0, mainWindow); + wstring pauseStringWS = selected + L" [PAUZA]"; LPCWSTR pauseString = pauseStringWS.c_str(); - EnableWindow(filesListCtrl, FALSE); + EnableWindow(filesListCtrl, TRUE); EnableWindow(playButton, TRUE); EnableWindow(stopButton, TRUE); EnableMenuItem(menuBar, ID_REFRESH, MF_DISABLED); @@ -349,6 +417,7 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { SetWindowText(playButton, L"Wznów"); break; } + currentState = (int)wParam; break; default: return DefWindowProc(hwnd, msg, wParam, lParam);