|
| 1 | +/* DrawBridge - aka ArduinoFloppyReader (and writer) |
| 2 | +* |
| 3 | +* Copyright (C) 2017-2024 Robert Smith (@RobSmithDev) |
| 4 | +* https://amiga.robsmithdev.co.uk |
| 5 | +* |
| 6 | +* This file is multi-licensed under the terms of the Mozilla Public |
| 7 | +* License Version 2.0 as published by Mozilla Corporation and the |
| 8 | +* GNU General Public License, version 2 or later, as published by the |
| 9 | +* Free Software Foundation. |
| 10 | +* |
| 11 | +* MPL2: https://www.mozilla.org/en-US/MPL/2.0/ |
| 12 | +* GPL2: https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html |
| 13 | +* |
| 14 | +*/ |
| 15 | + |
| 16 | +#include "stdafx.h" |
| 17 | +#include "ArduinoFloppyReaderWin.h" |
| 18 | +#include "CCleanDialog.h" |
| 19 | +#include "afxdialogex.h" |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | +// CCleanDialog dialog |
| 24 | + |
| 25 | +IMPLEMENT_DYNAMIC(CCleanDialog, CDialogEx) |
| 26 | + |
| 27 | +CCleanDialog::CCleanDialog(const std::wstring& comPort) : m_comPort(comPort), CDialogEx(IDD_CLEAN) { |
| 28 | +} |
| 29 | + |
| 30 | +CCleanDialog::~CCleanDialog() |
| 31 | +{ |
| 32 | + if (m_iothread) { |
| 33 | + if (m_iothread->joinable()) m_iothread->join(); |
| 34 | + delete m_iothread; |
| 35 | + m_iothread = nullptr; |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +void CCleanDialog::DoDataExchange(CDataExchange* pDX) |
| 40 | +{ |
| 41 | + CDialogEx::DoDataExchange(pDX); |
| 42 | + DDX_Control(pDX, IDC_MAKEYOUROWN, m_makeYourOwn); |
| 43 | + DDX_Control(pDX, ID_START, m_startButton); |
| 44 | + DDX_Control(pDX, 1007, m_progress); |
| 45 | +} |
| 46 | + |
| 47 | + |
| 48 | +BEGIN_MESSAGE_MAP(CCleanDialog, CDialogEx) |
| 49 | + ON_WM_SYSCOMMAND() |
| 50 | + ON_MESSAGE(WM_USER, &CCleanDialog::OnUser) |
| 51 | + ON_BN_CLICKED(ID_CLOSE, &CCleanDialog::OnBnClickedClose) |
| 52 | + ON_STN_CLICKED(IDC_MAKEYOUROWN, &CCleanDialog::OnStnClickedMakeyourown) |
| 53 | + ON_WM_SETCURSOR() |
| 54 | + ON_WM_CTLCOLOR() |
| 55 | + ON_BN_CLICKED(ID_START, &CCleanDialog::OnBnClickedStart) |
| 56 | +END_MESSAGE_MAP() |
| 57 | + |
| 58 | +void CCleanDialog::resetProgress(int max) { |
| 59 | + if (m_spTaskbarList) m_spTaskbarList->SetProgressState(GetSafeHwnd(), TBPF_INDETERMINATE); |
| 60 | + m_maxValue = max; |
| 61 | + m_stateNeedsToChange = true; |
| 62 | +}; |
| 63 | + |
| 64 | +void CCleanDialog::setProgress(int position) { |
| 65 | + m_progress.SetPos(position); |
| 66 | + if (m_stateNeedsToChange) { |
| 67 | + m_stateNeedsToChange = false; |
| 68 | + if (m_spTaskbarList) m_spTaskbarList->SetProgressState(GetSafeHwnd(), TBPF_NORMAL); |
| 69 | + } |
| 70 | + if (m_spTaskbarList) m_spTaskbarList->SetProgressValue(GetSafeHwnd(), position, m_maxValue); |
| 71 | +}; |
| 72 | + |
| 73 | +// CCleanDialog message handlers |
| 74 | +BOOL CCleanDialog::OnInitDialog() |
| 75 | +{ |
| 76 | + BOOL ret = CDialogEx::OnInitDialog(); |
| 77 | + |
| 78 | + HRESULT hr = ::CoCreateInstance(CLSID_TaskbarList, NULL, CLSCTX_INPROC_SERVER, __uuidof(ITaskbarList3), reinterpret_cast<void**>(&m_spTaskbarList)); |
| 79 | + if (SUCCEEDED(hr)) m_spTaskbarList->HrInit(); else m_spTaskbarList = nullptr; |
| 80 | + |
| 81 | + resetProgress(100); |
| 82 | + |
| 83 | + return ret; |
| 84 | +} |
| 85 | + |
| 86 | + |
| 87 | +BOOL CCleanDialog::PreTranslateMessage(MSG* pMsg) |
| 88 | +{ |
| 89 | + if (pMsg->message == WM_KEYDOWN) |
| 90 | + { |
| 91 | + if (pMsg->wParam == VK_RETURN || pMsg->wParam == VK_ESCAPE) |
| 92 | + { |
| 93 | + return !m_iothread; // Do not process further |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + if (pMsg->message == WM_CLOSE) { |
| 98 | + if (m_iothread) return 1; |
| 99 | + } |
| 100 | + |
| 101 | + return CWnd::PreTranslateMessage(pMsg); |
| 102 | +} |
| 103 | + |
| 104 | + |
| 105 | +void CCleanDialog::OnSysCommand(UINT nID, LPARAM lParam) |
| 106 | +{ |
| 107 | + if ((nID & 0xFFF0) == SC_CLOSE) |
| 108 | + { |
| 109 | + if (m_iothread) return; |
| 110 | + } |
| 111 | + |
| 112 | + CDialogEx::OnSysCommand(nID, lParam); |
| 113 | +} |
| 114 | + |
| 115 | +afx_msg LRESULT CCleanDialog::OnUser(WPARAM wParam, LPARAM lParam) |
| 116 | +{ |
| 117 | + if (wParam == 100) { |
| 118 | + if (lParam == 2) MessageBox(_T("No disk was detected in the drive. Clean aborted."), _T("Sorry"), MB_OK | MB_ICONEXCLAMATION); else |
| 119 | + if (lParam == 1) MessageBox(_T("Unable to communicate with the COM port. Please check port or run diagnostics."), _T("Sorry"), MB_OK | MB_ICONEXCLAMATION); else |
| 120 | + if (m_abortPressed) CDialogEx::OnCancel(); else MessageBox(L"Cleaning cycle completed.", L"Drive Head Cleaner", MB_OK | MB_ICONINFORMATION); |
| 121 | + if (m_iothread) { |
| 122 | + if (m_iothread->joinable()) m_iothread->join(); |
| 123 | + delete m_iothread; |
| 124 | + m_iothread = nullptr; |
| 125 | + } |
| 126 | + |
| 127 | + m_startButton.EnableWindow(true); |
| 128 | + } |
| 129 | + return 0; |
| 130 | +} |
| 131 | + |
| 132 | + |
| 133 | +void CCleanDialog::OnBnClickedClose() { |
| 134 | + if (m_iothread) m_abortPressed = true; else CDialogEx::OnCancel(); |
| 135 | +} |
| 136 | + |
| 137 | + |
| 138 | +void CCleanDialog::OnStnClickedMakeyourown() |
| 139 | +{ |
| 140 | + ShellExecute(GetSafeHwnd(), L"OPEN", L"https://youtu.be/7E4fSypg0pk", NULL, NULL, SW_SHOW); |
| 141 | +} |
| 142 | + |
| 143 | + |
| 144 | +BOOL CCleanDialog::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) |
| 145 | +{ |
| 146 | + if (pWnd->GetSafeHwnd() == m_makeYourOwn.GetSafeHwnd()) { |
| 147 | + SetCursor(LoadCursor(NULL, IDC_HAND)); |
| 148 | + return TRUE; |
| 149 | + } |
| 150 | + return CDialogEx::OnSetCursor(pWnd, nHitTest, message); |
| 151 | +} |
| 152 | + |
| 153 | + |
| 154 | +HBRUSH CCleanDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) |
| 155 | +{ |
| 156 | + HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor); |
| 157 | + |
| 158 | + if (nCtlColor == CTLCOLOR_STATIC && pWnd->GetSafeHwnd() == m_makeYourOwn.GetSafeHwnd()) pDC->SetTextColor(GetSysColor(COLOR_HOTLIGHT)); |
| 159 | + return hbr; |
| 160 | +} |
| 161 | + |
| 162 | +void CCleanDialog::OnBnClickedStart() |
| 163 | +{ |
| 164 | + if (m_iothread) return; |
| 165 | + m_startButton.EnableWindow(false); |
| 166 | + |
| 167 | + if (m_iothread) { |
| 168 | + if (m_iothread->joinable()) m_iothread->join(); |
| 169 | + delete m_iothread; |
| 170 | + m_iothread = nullptr; |
| 171 | + } |
| 172 | + |
| 173 | + m_iothread = new std::thread([this]() { |
| 174 | + |
| 175 | + resetProgress(1000); |
| 176 | + setProgress(0); |
| 177 | + |
| 178 | + ArduinoFloppyReader::ADFWriter reader; |
| 179 | + |
| 180 | + if (reader.openDevice(m_comPort)) { |
| 181 | + bool noDisk = reader.runClean([this](const uint16_t position, const uint16_t maxPosition) -> bool { |
| 182 | + if (position == 0) resetProgress(maxPosition); |
| 183 | + setProgress(position); |
| 184 | + return !m_abortPressed; |
| 185 | + }) == ArduinoFloppyReader::ADFResult::adfrCompletedWithErrors; |
| 186 | + reader.closeDevice(); |
| 187 | + PostMessage(WM_USER, 100, noDisk?2:0); |
| 188 | + } else PostMessage(WM_USER, 100, 1); |
| 189 | + }); |
| 190 | + |
| 191 | +} |
0 commit comments