From 1e765e63062a3183dc65671aca6192abe6b8146c Mon Sep 17 00:00:00 2001 From: GRZ4NA Date: Mon, 31 May 2021 22:18:23 +0200 Subject: [PATCH] Massive structure rework --- ClickPlay.rc | Bin 8360 -> 9324 bytes Resource.h | 5 +- main.cpp | 297 +++++++++++++++++++++++++++++++-------------------- 3 files changed, 185 insertions(+), 117 deletions(-) diff --git a/ClickPlay.rc b/ClickPlay.rc index ed5baa2dc84e063a80714a9308d59577f3734c92..3f1cd8123e87de40cdf12926146f4ce649165b73 100644 GIT binary patch delta 835 zcmZ4C_{L*{1}C#CL&W6wGSZXpaq~?M;N+RC!lgBN0y~en8G|Vh8UwKogD-;{LkL4W zLokpGV(?_}V{nIx1_MQ1fqX{>Ck7uN%VlyjXTL6KItn>8>!TPF8FCr28S)r18L}7@ z7}6N>8FCm>7*ZLE81%p}hM|PP8HkI3;@Uv>m@p^--Jk%3o(wKPR|Ej<_hj$}vYde| zf1q1|t^tY!5#xR?Bo6?cGg*ksheS{8=hmIv#YK(abDn&ID@qn`kWV(|?j_lM*3hWs zq9AJRCtL7D1!8lX0z(ZjDqu38MB>ii0ZcAl458p;;|YwiNSuiV6y`^G0%&0Jes1x} zT9RCoFUYV=J|VVY@+aP3le75qCg0)fV>Fq(kWX}S13!l}p2RbGIiDi0GcZB 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);