From 4d05747a62c442c246828e0205ac249f655bcd6f Mon Sep 17 00:00:00 2001 From: lx-s Date: Mon, 1 Aug 2022 14:52:47 +0200 Subject: [PATCH] Further BT UI work. --- WinMute/SettingsDlg.cpp | 2 + WinMute/Settings_BluetoothDlg.cpp | 293 ++++++++++++++++++++++++++++++ WinMute/Settings_WifiDlg.cpp | 8 +- WinMute/WinMute.rc | 24 +++ WinMute/WinMute.vcxproj | 9 +- WinMute/WinMute.vcxproj.filters | 5 +- WinMute/resource.h | 4 +- 7 files changed, 335 insertions(+), 10 deletions(-) create mode 100644 WinMute/Settings_BluetoothDlg.cpp diff --git a/WinMute/SettingsDlg.cpp b/WinMute/SettingsDlg.cpp index 717fe1e..5cbb4ea 100644 --- a/WinMute/SettingsDlg.cpp +++ b/WinMute/SettingsDlg.cpp @@ -58,6 +58,8 @@ struct SettingsDlgData { extern INT_PTR CALLBACK Settings_QuietHoursDlgProc(HWND, UINT, WPARAM, LPARAM); extern INT_PTR CALLBACK Settings_GeneralDlgProc(HWND, UINT, WPARAM, LPARAM); extern INT_PTR CALLBACK Settings_MuteDlgProc(HWND, UINT, WPARAM, LPARAM); +extern INT_PTR CALLBACK Settings_BluetoothDlgProc(HWND, UINT, WPARAM, LPARAM); +extern INT_PTR CALLBACK Settings_WifiDlgProc(HWND, UINT, WPARAM, LPARAM); extern INT_PTR CALLBACK Settings_GeneralWifiDlgProc(HWND, UINT, WPARAM, LPARAM); extern HINSTANCE hglobInstance; diff --git a/WinMute/Settings_BluetoothDlg.cpp b/WinMute/Settings_BluetoothDlg.cpp new file mode 100644 index 0000000..4538184 --- /dev/null +++ b/WinMute/Settings_BluetoothDlg.cpp @@ -0,0 +1,293 @@ +/* + WinMute + Copyright (c) 2022, Alexander Steinhoefer + +----------------------------------------------------------------------------- +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + * Neither the name of the author nor the names of its contributors may + be used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. +----------------------------------------------------------------------------- +*/ + +#include "common.h" + +static constexpr int SSID_MAX_LEN = 32; + +struct WiFiData { + tstring ssidName; +}; + +static INT_PTR CALLBACK Settings_BluetoothAddDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam) +{ + switch (msg) { + case WM_INITDIALOG: { + HWND hSsid = GetDlgItem(hDlg, IDC_WIFI_NAME); + WiFiData* wifiData = reinterpret_cast(lParam); + if (wifiData == nullptr) { + return FALSE; + } + SetWindowLongPtr(hDlg, GWLP_USERDATA, reinterpret_cast(wifiData)); + + if (wifiData->ssidName.length() == 0) { + if (!SetWindowText(hDlg, _T("Add WiFi network"))) { + PrintWindowsError(_T("SetWindowText"), GetLastError()); + return FALSE; + } + } + else { + if (!SetWindowText(hDlg, _T("Edit WiFi network")) + || !SetWindowText( + GetDlgItem(hDlg, IDC_WIFI_NAME), + wifiData->ssidName.c_str())) { + PrintWindowsError(_T("SetWindowText"), GetLastError()); + return FALSE; + } + } + Edit_LimitText(hSsid, SSID_MAX_LEN); + if (GetDlgCtrlID(reinterpret_cast(wParam)) != IDC_WIFI_NAME) { + SetFocus(hSsid); + return FALSE; + } + return TRUE; + } + case WM_COMMAND: + if (LOWORD(wParam) == IDOK) { + HWND hSsid = GetDlgItem(hDlg, IDC_WIFI_NAME); + int textLen = Edit_GetTextLength(hSsid); + if (textLen == 0) { + EDITBALLOONTIP ebt; + ZeroMemory(&ebt, sizeof(ebt)); + ebt.cbStruct = sizeof(ebt); + ebt.pszText = _T("Please enter a SSID/WIFI name"); + ebt.pszTitle = _T("SSID Name"); + ebt.ttiIcon = TTI_INFO; + Edit_ShowBalloonTip(hSsid, &ebt); + } + else { + TCHAR ssidBuf[SSID_MAX_LEN + 1]; + WiFiData* wifiData = reinterpret_cast(GetWindowLongPtr(hDlg, GWLP_USERDATA)); + if (wifiData != nullptr) { + Edit_GetText(hSsid, ssidBuf, ARRAY_SIZE(ssidBuf)); + wifiData->ssidName = ssidBuf; + EndDialog(hDlg, 0); + } + else { + EndDialog(hDlg, 1); + } + } + } + else if (LOWORD(wParam) == IDCANCEL) { + EndDialog(hDlg, 1); + } + return FALSE; + case WM_CLOSE: + EndDialog(hDlg, 1); + return TRUE; + default: + break; + } + return FALSE; +} + +static std::vector ExportSsidListItems(HWND hList) +{ + std::vector items; + DWORD itemCount = ListBox_GetCount(hList); + for (DWORD i = 0; i < itemCount; ++i) { + TCHAR textBuf[SSID_MAX_LEN + 1] = { 0 }; + DWORD textLen = ListBox_GetTextLen(hList, i); + if (textLen < ARRAY_SIZE(textBuf)) { + ListBox_GetText(hList, i, textBuf); + items.push_back(textBuf); + } + } + return items; +} + +static bool IsBluetoothAvailable() +{ + HANDLE wlanHandle; + DWORD vers = 2; + if (WlanOpenHandle(vers, NULL, &vers, &wlanHandle) != ERROR_SUCCESS) { + return false; + } + WlanCloseHandle(wlanHandle, NULL); + return true; +} + +INT_PTR CALLBACK Settings_BluetoothDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam) +{ + switch (msg) { + case WM_INITDIALOG: + { + if (IsAppThemed()) { + EnableThemeDialogTexture(hDlg, ETDT_ENABLETAB); + } + WMSettings* settings = reinterpret_cast(lParam); + assert(settings != nullptr); + SetWindowLongPtr(hDlg, GWLP_USERDATA, reinterpret_cast(settings)); + + DWORD enabled = settings->QueryValue(SettingsKey::MUTE_ON_WLAN); + Button_SetCheck(GetDlgItem(hDlg, IDC_ENABLE_WIFI_MUTE), + enabled ? BST_CHECKED : BST_UNCHECKED); + Button_Enable(GetDlgItem(hDlg, IDC_IS_PERMITLIST), enabled); + enabled = settings->QueryValue(SettingsKey::MUTE_ON_WLAN_ALLOWLIST); + Button_SetCheck(GetDlgItem(hDlg, IDC_IS_PERMITLIST), + enabled ? BST_CHECKED : BST_UNCHECKED); + + + Button_Enable(GetDlgItem(hDlg, IDC_WIFI_EDIT), FALSE); + Button_Enable(GetDlgItem(hDlg, IDC_WIFI_REMOVE), FALSE); + + HWND hList = GetDlgItem(hDlg, IDC_WIFI_LIST); + const auto networks = settings->GetWifiNetworks(); + for (const auto& n : networks) { + ListBox_AddString(hList, n.c_str()); + } + Button_Enable(GetDlgItem(hDlg, IDC_WIFI_REMOVEALL), + ListBox_GetCount(hList) > 0); + + if (!IsBluetoothAvailable()) { + ShowWindow(GetDlgItem(hDlg, IDC_STATIC_WLAN_NOT_AVAILABLE), SW_SHOW); + Button_SetCheck(GetDlgItem(hDlg, IDC_ENABLE_WIFI_MUTE), BST_UNCHECKED); + Button_Enable(GetDlgItem(hDlg, IDC_ENABLE_WIFI_MUTE), FALSE); + Button_SetCheck(GetDlgItem(hDlg, IDC_IS_PERMITLIST), BST_UNCHECKED); + Button_Enable(GetDlgItem(hDlg, IDC_IS_PERMITLIST), FALSE); + } + else { + ShowWindow(GetDlgItem(hDlg, IDC_STATIC_WLAN_NOT_AVAILABLE), SW_HIDE); + } + return TRUE; + } + case WM_COMMAND: + { + if (LOWORD(wParam) == IDC_ENABLE_WIFI_MUTE) { + DWORD checked = Button_GetCheck(GetDlgItem(hDlg, IDC_ENABLE_WIFI_MUTE)); + Button_Enable(GetDlgItem(hDlg, IDC_IS_PERMITLIST), checked == BST_CHECKED); + } + else if (LOWORD(wParam) == IDC_BLUETOOTH_LIST) { + HWND hList = GetDlgItem(hDlg, IDC_BLUETOOTH_LIST); + if (HIWORD(wParam) == LBN_SELCHANGE || HIWORD(wParam) == LBN_SELCANCEL) { + bool entrySelected = (ListBox_GetCurSel(hList) != LB_ERR); + Button_Enable(GetDlgItem(hDlg, IDC_WIFI_EDIT), entrySelected); + Button_Enable(GetDlgItem(hDlg, IDC_WIFI_REMOVE), entrySelected); + } + else if (HIWORD(wParam) == LBN_KILLFOCUS) { + bool entrySelected = (ListBox_GetCurSel(hList) != LB_ERR); + Button_Enable(GetDlgItem(hDlg, IDC_WIFI_EDIT), entrySelected); + Button_Enable(GetDlgItem(hDlg, IDC_WIFI_REMOVE), entrySelected); + } + } + else if (LOWORD(wParam) == IDC_BLUETOOTH_ADD) { + WiFiData wifiData; + if (DialogBoxParam( + NULL, + MAKEINTRESOURCE(IDD_SETTINGS_BLUETOOTH_ADD), + hDlg, + Settings_BluetoothAddDlgProc, + reinterpret_cast(&wifiData)) == 0) { + std::vector networks = ExportSsidListItems(GetDlgItem(hDlg, IDC_BLUETOOTH_LIST)); + if (std::find(begin(networks), end(networks), wifiData.ssidName) == end(networks)) { + ListBox_AddString( + GetDlgItem(hDlg, IDC_BLUETOOTH_LIST), + wifiData.ssidName.c_str()); + HWND hRemoveAll = GetDlgItem(hDlg, IDC_WIFI_REMOVEALL); + if (!IsWindowEnabled(hRemoveAll)) { + Button_Enable(hRemoveAll, TRUE); + } + } + } + } + else if (LOWORD(wParam) == IDC_BLUETOOTH_EDIT) { + HWND hList = GetDlgItem(hDlg, IDC_WIFI_LIST); + WPARAM sel = ListBox_GetCurSel(hList); + if (sel != LB_ERR) { + int len = ListBox_GetTextLen(hList, sel); + TCHAR* textBuf = NULL; + if (len != LB_ERR) { + if ((textBuf = new TCHAR[static_cast(len) + 1]) != NULL) { + ListBox_GetText(hList, sel, textBuf); + + WiFiData wifiData; + wifiData.ssidName = textBuf; + delete[] textBuf; + + if (DialogBoxParam( + NULL, + MAKEINTRESOURCE(IDD_SETTINGS_BLUETOOTH_ADD), + hDlg, + Settings_BluetoothAddDlgProc, + reinterpret_cast(&wifiData)) == 0) { + std::vector networks = ExportSsidListItems(GetDlgItem(hDlg, IDC_WIFI_LIST)); + if (std::find(begin(networks), end(networks), wifiData.ssidName) == end(networks)) { + ListBox_InsertString(hList, sel, wifiData.ssidName.c_str()); + ListBox_DeleteString(hList, sel + 1); + } + else { + ListBox_DeleteString(hList, sel); + } + } + } + } + } + } + else if (LOWORD(wParam) == IDC_WIFI_REMOVE) { + HWND hList = GetDlgItem(hDlg, IDC_WIFI_LIST); + WPARAM sel = ListBox_GetCurSel(hList); + if (sel != LB_ERR) { + ListBox_DeleteString(hList, sel); + if (ListBox_GetCount(hList) == 0) { + Button_Enable(GetDlgItem(hDlg, IDC_BLUETOOTH_EDIT), FALSE); + Button_Enable(GetDlgItem(hDlg, IDC_BLUETOOTH_REMOVE), FALSE); + Button_Enable(GetDlgItem(hDlg, IDC_BLUETOOTH_REMOVEALL), FALSE); + } + } + } + else if (LOWORD(wParam) == IDC_BLUETOOTH_REMOVEALL) { + ListBox_ResetContent(GetDlgItem(hDlg, IDC_BLUETOOTH_LIST)); + Button_Enable(GetDlgItem(hDlg, IDC_BLUETOOTH_EDIT), FALSE); + Button_Enable(GetDlgItem(hDlg, IDC_BLUETOOTH_REMOVE), FALSE); + Button_Enable(GetDlgItem(hDlg, IDC_BLUETOOTH_REMOVEALL), FALSE); + } + return 0; + } + case WM_SAVESETTINGS: { + /*WMSettings* settings = reinterpret_cast(GetWindowLongPtr(hDlg, GWLP_USERDATA)); + std::vector networks = ExportSsidListItems(GetDlgItem(hDlg, IDC_WIFI_LIST)); + settings->StoreWifiNetworks(networks); + + DWORD checked = Button_GetCheck(GetDlgItem(hDlg, IDC_ENABLE_WIFI_MUTE)); + settings->SetValue(SettingsKey::MUTE_ON_WLAN, checked == BST_CHECKED); + + checked = Button_GetCheck(GetDlgItem(hDlg, IDC_IS_PERMITLIST)); + settings->SetValue(SettingsKey::MUTE_ON_WLAN_ALLOWLIST, checked == BST_CHECKED);*/ + + return 0; + } + default: + break; + } + return FALSE; +} diff --git a/WinMute/Settings_WifiDlg.cpp b/WinMute/Settings_WifiDlg.cpp index b196ba7..f0eaf9c 100644 --- a/WinMute/Settings_WifiDlg.cpp +++ b/WinMute/Settings_WifiDlg.cpp @@ -39,7 +39,7 @@ struct WiFiData { tstring ssidName; }; -INT_PTR CALLBACK Settings_GeneralWifiAddDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam) +INT_PTR CALLBACK Settings_WifiAddDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_INITDIALOG: { @@ -133,7 +133,7 @@ static bool IsWlanAvailable() return true; } -INT_PTR CALLBACK Settings_GeneralWifiDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam) +INT_PTR CALLBACK Settings_WifiDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_INITDIALOG: @@ -199,7 +199,7 @@ INT_PTR CALLBACK Settings_GeneralWifiDlgProc(HWND hDlg, UINT msg, WPARAM wParam, NULL, MAKEINTRESOURCE(IDD_SETTINGS_WIFI_ADD), hDlg, - Settings_GeneralWifiAddDlgProc, + Settings_WifiAddDlgProc, reinterpret_cast(&wifiData)) == 0) { std::vector networks = ExportSsidListItems(GetDlgItem(hDlg, IDC_WIFI_LIST)); if (std::find(begin(networks), end(networks), wifiData.ssidName) == end(networks)) { @@ -230,7 +230,7 @@ INT_PTR CALLBACK Settings_GeneralWifiDlgProc(HWND hDlg, UINT msg, WPARAM wParam, NULL, MAKEINTRESOURCE(IDD_SETTINGS_WIFI_ADD), hDlg, - Settings_GeneralWifiAddDlgProc, + Settings_WifiAddDlgProc, reinterpret_cast(&wifiData)) == 0) { std::vector networks = ExportSsidListItems(GetDlgItem(hDlg, IDC_WIFI_LIST)); if (std::find(begin(networks), end(networks), wifiData.ssidName) == end(networks)) { diff --git a/WinMute/WinMute.rc b/WinMute/WinMute.rc index 35f3123..688e504 100644 --- a/WinMute/WinMute.rc +++ b/WinMute/WinMute.rc @@ -190,6 +190,17 @@ BEGIN LTEXT """Bluetooth Muting"" enables muting based on the presence of a bluetooth audio-class device.\nWinMute mutes your workstation if an audio-class bluetooth device is disconnected from your PC.",IDC_STATIC,7,0,228,31 END +IDD_SETTINGS_BLUETOOTH_ADD DIALOGEX 0, 0, 201, 62 +STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Dialog" +FONT 8, "MS Shell Dlg", 400, 0, 0x1 +BEGIN + DEFPUSHBUTTON "Save",IDOK,91,41,50,14 + PUSHBUTTON "Cancel",IDCANCEL,144,41,50,14 + LTEXT "Bluetooth device name:",IDC_STATIC,7,7,187,14 + EDITTEXT IDC_WIFI_NAME,7,20,187,16,ES_AUTOHSCROLL +END + ///////////////////////////////////////////////////////////////////////////// // @@ -278,6 +289,14 @@ BEGIN TOPMARGIN, 7 BOTTOMMARGIN, 194 END + + IDD_SETTINGS_BLUETOOTH_ADD, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 194 + TOPMARGIN, 7 + BOTTOMMARGIN, 55 + END END #endif // APSTUDIO_INVOKED @@ -338,6 +357,11 @@ BEGIN 0 END +IDD_SETTINGS_BLUETOOTH_ADD AFX_DIALOG_LAYOUT +BEGIN + 0 +END + ///////////////////////////////////////////////////////////////////////////// // diff --git a/WinMute/WinMute.vcxproj b/WinMute/WinMute.vcxproj index 86b2fc6..26f0636 100644 --- a/WinMute/WinMute.vcxproj +++ b/WinMute/WinMute.vcxproj @@ -110,7 +110,7 @@ Windows true - strsafe.lib;comctl32.lib;WtsApi32.lib;Winmm.lib;UxTheme.lib;comsuppwd.lib;Wlanapi.lib;Bthprops.lib;Version.lib;%(AdditionalDependencies) + strsafe.lib;comctl32.lib;WtsApi32.lib;Winmm.lib;UxTheme.lib;comsuppwd.lib;Wlanapi.lib;Version.lib;%(AdditionalDependencies) PerMonitorHighDPIAware @@ -136,7 +136,7 @@ true true true - strsafe.lib;comctl32.lib;WtsApi32.lib;Winmm.lib;UxTheme.lib;comsuppwd.lib;Wlanapi.lib;Bthprops.lib;Version.lib;%(AdditionalDependencies) + strsafe.lib;comctl32.lib;WtsApi32.lib;Winmm.lib;UxTheme.lib;comsuppwd.lib;Wlanapi.lib;Version.lib;%(AdditionalDependencies) PerMonitorHighDPIAware @@ -157,7 +157,7 @@ Windows true - strsafe.lib;comctl32.lib;WtsApi32.lib;Winmm.lib;UxTheme.lib;comsuppwd.lib;Wlanapi.lib;Bthprops.lib;Version.lib;%(AdditionalDependencies) + Bthprops.lib;comsuppwd.lib;comctl32.lib;strsafe.lib;UxTheme.lib;Winmm.lib;Wlanapi.lib;WtsApi32.lib;Version.lib;%(AdditionalDependencies) PerMonitorHighDPIAware @@ -183,7 +183,7 @@ true true true - strsafe.lib;comctl32.lib;WtsApi32.lib;Winmm.lib;UxTheme.lib;comsuppwd.lib;Wlanapi.lib;Bthprops.lib;Version.lib;%(AdditionalDependencies) + Bthprops.lib;comsuppwd.lib;comctl32.lib;strsafe.lib;UxTheme.lib;Winmm.lib;Wlanapi.lib;WtsApi32.lib;Version.lib;%(AdditionalDependencies) PerMonitorHighDPIAware @@ -225,6 +225,7 @@ Create Create + diff --git a/WinMute/WinMute.vcxproj.filters b/WinMute/WinMute.vcxproj.filters index d9f986e..7c520e6 100644 --- a/WinMute/WinMute.vcxproj.filters +++ b/WinMute/WinMute.vcxproj.filters @@ -55,7 +55,7 @@ {a804b525-296b-455b-a1bc-d8fdbf5a9c3d} - {63e91d90-aeee-461f-9783-6e1e42eee65b} + {5cab523c-1a5a-4429-b495-b630124865d6} @@ -188,6 +188,9 @@ Source Files\Controllers\WiFi + + Source Files\UI\Dialogs\Settings + Source Files\Controllers\Bluetooth diff --git a/WinMute/resource.h b/WinMute/resource.h index 39950e0..f7b4594 100644 --- a/WinMute/resource.h +++ b/WinMute/resource.h @@ -18,6 +18,8 @@ #define IDD_SETTINGS_WIFI 127 #define IDD_SETTINGS_WIFI_ADD 129 #define IDD_SETTINGS_BLUETOOTH 131 +#define IDD_DIALOG1 133 +#define IDD_SETTINGS_BLUETOOTH_ADD 133 #define IDC_LINK_HOMEPAGE 1001 #define IDC_LINK_SUPPORT 1002 #define IDC_LINK_PROJECT 1002 @@ -102,7 +104,7 @@ // #ifdef APSTUDIO_INVOKED #ifndef APSTUDIO_READONLY_SYMBOLS -#define _APS_NEXT_RESOURCE_VALUE 133 +#define _APS_NEXT_RESOURCE_VALUE 135 #define _APS_NEXT_COMMAND_VALUE 40033 #define _APS_NEXT_CONTROL_VALUE 1036 #define _APS_NEXT_SYMED_VALUE 101 -- 2.43.0