project64/Source/Project64/UserInterface/Settings/SettingsPage-KeyboardShortc...

392 lines
14 KiB
C++
Raw Normal View History

2016-01-27 09:11:59 +00:00
#include "stdafx.h"
#include "SettingsPage.h"
COptionsShortCutsPage::COptionsShortCutsPage(HWND hParent, const RECT & rcDispay) :
m_EnableReset(false)
2016-01-27 09:11:59 +00:00
{
if (!Create(hParent, rcDispay))
{
return;
}
2020-05-12 12:19:05 +00:00
SetDlgItemText(IDC_S_CPU_STATE, wGS(ACCEL_CPUSTATE_TITLE).c_str());
SetDlgItemText(IDC_MENU_ITEM_TEXT, wGS(ACCEL_MENUITEM_TITLE).c_str());
SetDlgItemText(IDC_S_CURRENT_KEYS, wGS(ACCEL_CURRENTKEYS_TITLE).c_str());
SetDlgItemText(IDC_S_SELECT_SHORT, wGS(ACCEL_SELKEY_TITLE).c_str());
SetDlgItemText(IDC_S_CURRENT_ASSIGN, wGS(ACCEL_ASSIGNEDTO_TITLE).c_str());
SetDlgItemText(IDC_ASSIGN, wGS(ACCEL_ASSIGN_BTN).c_str());
SetDlgItemText(IDC_REMOVE, wGS(ACCEL_REMOVE_BTN).c_str());
SetDlgItemText(IDC_KEY_PROMPT, wGS(ACCEL_DETECTKEY).c_str());
2016-01-27 09:11:59 +00:00
m_CreateNewShortCut.AttachToDlgItem(m_hWnd, IDC_S_SELECT_SHORT);
m_CpuState.Attach(GetDlgItem(IDC_C_CPU_STATE));
m_MenuItems.Attach(GetDlgItem(IDC_MENU_ITEMS));
m_CurrentKeys.Attach(GetDlgItem(IDC_CURRENT_KEYS));
m_VirtualKeyList.Attach(GetDlgItem(IDC_VIRTUALKEY));
m_MenuItems.ModifyStyle(0, TVS_SHOWSELALWAYS);
m_CpuState.SetItemData(m_CpuState.AddString(wGS(ACCEL_CPUSTATE_1).c_str()), CMenuShortCutKey::RUNNING_STATE_NOT_RUNNING);
m_CpuState.SetItemData(m_CpuState.AddString(wGS(ACCEL_CPUSTATE_3).c_str()), CMenuShortCutKey::RUNNING_STATE_WINDOWED);
m_CpuState.SetItemData(m_CpuState.AddString(wGS(ACCEL_CPUSTATE_4).c_str()), CMenuShortCutKey::RUNNING_STATE_FULLSCREEN);
2016-01-27 09:11:59 +00:00
m_CpuState.SetCurSel(0);
int VirtualKeyListSize;
VIRTUAL_KEY * VirtualKeyList = CMenuShortCutKey::VirtualKeyList(VirtualKeyListSize);
for (int count = 0; count < VirtualKeyListSize; count++)
{
m_VirtualKeyList.SetItemData(m_VirtualKeyList.AddString(VirtualKeyList[count].Name), VirtualKeyList[count].Key);
}
OnCpuStateChanged(LBN_SELCHANGE, IDC_C_CPU_STATE, GetDlgItem(IDC_C_CPU_STATE));
CheckResetEnable();
}
void COptionsShortCutsPage::CheckResetEnable(void)
{
2021-04-26 10:48:20 +00:00
const MSC_MAP & ShortCuts = m_ShortCuts.GetShortCuts();
for (MSC_MAP::const_iterator Item = ShortCuts.begin(); Item != ShortCuts.end(); Item++)
2016-01-27 09:11:59 +00:00
{
const SHORTCUT_KEY_LIST & ShortCutList = Item->second.GetAccelItems();
for (SHORTCUT_KEY_LIST::const_iterator ShortCut_item = ShortCutList.begin(); ShortCut_item != ShortCutList.end(); ShortCut_item++)
{
if (!ShortCut_item->Inactive() && !ShortCut_item->UserAdded())
{
continue;
}
m_EnableReset = true;
return;
}
}
m_EnableReset = false;
}
void COptionsShortCutsPage::OnCpuStateChanged(UINT /*Code*/, int /*id*/, HWND /*ctl*/)
{
2017-04-29 05:55:30 +00:00
RUNNING_STATE RunningState = (RUNNING_STATE)m_CpuState.GetItemData(m_CpuState.GetCurSel());
2016-01-27 09:11:59 +00:00
2021-04-26 10:48:20 +00:00
const MSC_MAP & ShortCuts = m_ShortCuts.GetShortCuts();
2016-01-27 09:11:59 +00:00
m_MenuItems.DeleteAllItems();
2021-04-26 10:48:20 +00:00
for (MSC_MAP::const_iterator Item = ShortCuts.begin(); Item != ShortCuts.end(); Item++)
2016-01-27 09:11:59 +00:00
{
2017-04-29 05:55:30 +00:00
if (!Item->second.Avaliable(RunningState))
2016-01-27 09:11:59 +00:00
{
continue;
}
2017-04-29 05:55:30 +00:00
// Find parent
2016-01-27 09:11:59 +00:00
HTREEITEM hParent = m_MenuItems.GetChildItem(TVI_ROOT);
while (hParent)
{
if (m_MenuItems.GetItemData(hParent) == (DWORD_PTR)Item->second.Section())
{
break;
}
hParent = m_MenuItems.GetNextSiblingItem(hParent);
}
2021-04-12 11:35:39 +00:00
if (hParent == nullptr)
2016-01-27 09:11:59 +00:00
{
hParent = m_MenuItems.InsertItem(TVIF_TEXT | TVIF_PARAM, wGS(Item->second.Section()).c_str(), 0, 0, 0, 0, Item->second.Section(), TVI_ROOT, TVI_LAST);
2016-01-27 09:11:59 +00:00
}
wstring str = wGS(Item->second.Title());
std::wstring::size_type pos = str.find(L"&");
while (pos != std::wstring::npos)
{
str.replace(pos, 1, L"");
pos = str.find(L"&", pos);
}
pos = str.find(L"...");
while (pos != std::wstring::npos)
{
str.replace(pos, 3, L"");
pos = str.find(L"...", pos);
}
HTREEITEM hItem = m_MenuItems.InsertItem(TVIF_TEXT | TVIF_PARAM, str.c_str(), 0, 0, 0, 0, (DWORD_PTR)&Item->second, hParent, TVI_LAST);
2016-01-27 09:11:59 +00:00
const SHORTCUT_KEY_LIST & ShortCutList = Item->second.GetAccelItems();
for (SHORTCUT_KEY_LIST::const_iterator ShortCut_item = ShortCutList.begin(); ShortCut_item != ShortCutList.end(); ShortCut_item++)
{
if (!ShortCut_item->Inactive() && !ShortCut_item->UserAdded())
{
continue;
}
m_MenuItems.SetItemState(hItem, TVIS_BOLD, TVIS_BOLD);
m_MenuItems.SetItemState(hParent, TVIS_BOLD, TVIS_BOLD);
break;
}
}
}
void COptionsShortCutsPage::OnRemoveClicked(UINT /*Code*/, int /*id*/, HWND /*ctl*/)
{
HTREEITEM hSelectedItem = m_MenuItems.GetSelectedItem();
2021-04-12 11:35:39 +00:00
if (hSelectedItem == nullptr)
2016-01-27 09:11:59 +00:00
{
g_Notify->DisplayWarning(GS(MSG_NO_SEL_SHORTCUT));
2016-01-27 09:11:59 +00:00
return;
}
HTREEITEM hParent = m_MenuItems.GetParentItem(hSelectedItem);
2021-04-12 11:35:39 +00:00
if (hParent == nullptr)
2016-01-27 09:11:59 +00:00
{
g_Notify->DisplayWarning(GS(MSG_NO_SEL_SHORTCUT));
2016-01-27 09:11:59 +00:00
return;
}
CShortCutItem * ShortCut = (CShortCutItem *)m_MenuItems.GetItemData(hSelectedItem);
// Make sure an item is selected
2016-01-27 09:11:59 +00:00
int index = m_CurrentKeys.GetCurSel();
if (index < 0)
{
g_Notify->DisplayWarning(GS(MSG_NO_SEL_SHORTCUT));
2016-01-27 09:11:59 +00:00
return;
}
ShortCut->RemoveItem((CMenuShortCutKey *)m_CurrentKeys.GetItemData(index));
m_MenuItems.SetItemState(hSelectedItem, TVIS_BOLD, TVIS_BOLD);
m_MenuItems.SetItemState(hParent, TVIS_BOLD, TVIS_BOLD);
m_EnableReset = true;
RefreshShortCutOptions(hSelectedItem);
SendMessage(GetParent(), PSM_CHANGED, (WPARAM)m_hWnd, 0);
}
void COptionsShortCutsPage::OnDetectKeyClicked(UINT /*Code*/, int /*id*/, HWND /*ctl*/)
{
2021-04-12 11:35:39 +00:00
CloseHandle(CreateThread(nullptr, 0, (LPTHREAD_START_ROUTINE)stInputGetKeys, this, 0, nullptr));
2016-01-27 09:11:59 +00:00
}
void COptionsShortCutsPage::OnAssignClicked(UINT /*Code*/, int /*id*/, HWND /*ctl*/)
{
// Get the virtual key info
2016-01-27 09:11:59 +00:00
int index = m_VirtualKeyList.GetCurSel();
if (index < 0)
{
g_Notify->DisplayWarning(GS(MSG_NO_SHORTCUT_SEL));
2016-01-27 09:11:59 +00:00
return;
}
WORD key = (WORD)SendDlgItemMessage(IDC_VIRTUALKEY, CB_GETITEMDATA, index, 0);
bool bCtrl = (SendDlgItemMessage(IDC_CTL, BM_GETCHECK, 0, 0) == BST_CHECKED);
bool bAlt = (SendDlgItemMessage(IDC_ALT, BM_GETCHECK, 0, 0) == BST_CHECKED);
bool bShift = (SendDlgItemMessage(IDC_SHIFT, BM_GETCHECK, 0, 0) == BST_CHECKED);
2017-04-29 05:55:30 +00:00
RUNNING_STATE RunningState = (RUNNING_STATE)m_CpuState.GetItemData(m_CpuState.GetCurSel());
2016-01-27 09:11:59 +00:00
HTREEITEM hSelectedItem = m_MenuItems.GetSelectedItem();
2021-04-12 11:35:39 +00:00
if (hSelectedItem == nullptr)
2016-01-27 09:11:59 +00:00
{
g_Notify->DisplayWarning(GS(MSG_NO_MENUITEM_SEL));
2016-01-27 09:11:59 +00:00
return;
}
HTREEITEM hParent = m_MenuItems.GetParentItem(hSelectedItem);
2021-04-12 11:35:39 +00:00
if (hParent == nullptr)
2016-01-27 09:11:59 +00:00
{
g_Notify->DisplayWarning(GS(MSG_NO_MENUITEM_SEL));
2016-01-27 09:11:59 +00:00
return;
}
CShortCutItem * ShortCut = (CShortCutItem *)m_MenuItems.GetItemData(hSelectedItem);
2017-04-29 05:55:30 +00:00
LanguageStringID strid = m_ShortCuts.GetMenuItemName(key, bCtrl, bAlt, bShift, RunningState);
2016-01-27 09:11:59 +00:00
if (strid != EMPTY_STRING)
{
g_Notify->DisplayWarning(GS(MSG_MENUITEM_ASSIGNED));
2016-01-27 09:11:59 +00:00
return;
}
2017-04-29 05:55:30 +00:00
CMenuShortCutKey::ACCESS_MODE AccessMode = CMenuShortCutKey::ACCESS_NONE;
switch (RunningState)
{
case CMenuShortCutKey::RUNNING_STATE_NOT_RUNNING: AccessMode = CMenuShortCutKey::ACCESS_GAME_NOT_RUNNING; break;
case CMenuShortCutKey::RUNNING_STATE_WINDOWED: AccessMode = CMenuShortCutKey::ACCESS_GAME_RUNNING; break;
case CMenuShortCutKey::RUNNING_STATE_FULLSCREEN: AccessMode = CMenuShortCutKey::ACCESS_GAME_RUNNING_FULLSCREEN; break;
}
ShortCut->AddShortCut(key, bCtrl, bAlt, bShift, AccessMode, true, false);
2016-01-27 09:11:59 +00:00
m_MenuItems.SetItemState(hSelectedItem, TVIS_BOLD, TVIS_BOLD);
m_MenuItems.SetItemState(hParent, TVIS_BOLD, TVIS_BOLD);
m_EnableReset = true;
RefreshShortCutOptions(hSelectedItem);
SendMessage(GetParent(), PSM_CHANGED, (WPARAM)m_hWnd, 0);
}
void COptionsShortCutsPage::OnShortCutChanged(UINT /*Code*/, int /*id*/, HWND /*ctl*/)
{
// Get the virtual key info
2016-01-27 09:11:59 +00:00
int index = m_VirtualKeyList.GetCurSel();
2022-09-21 05:16:07 +00:00
if (index < 0)
{
return;
}
2016-01-27 09:11:59 +00:00
WORD key = (WORD)m_VirtualKeyList.GetItemData(index);
bool bCtrl = (SendDlgItemMessage(IDC_CTL, BM_GETCHECK, 0, 0) == BST_CHECKED);
bool bAlt = (SendDlgItemMessage(IDC_ALT, BM_GETCHECK, 0, 0) == BST_CHECKED);
bool bShift = (SendDlgItemMessage(IDC_SHIFT, BM_GETCHECK, 0, 0) == BST_CHECKED);
2017-04-29 05:55:30 +00:00
RUNNING_STATE RunningState = (RUNNING_STATE)m_CpuState.GetItemData(m_CpuState.GetCurSel());
2016-01-27 09:11:59 +00:00
2017-04-29 05:55:30 +00:00
stdstr str = GS(m_ShortCuts.GetMenuItemName(key, bCtrl, bAlt, bShift, RunningState));
2016-01-27 09:11:59 +00:00
if (str.length() > 0)
{
str.resize(std::remove(str.begin(), str.end(), '&') - str.begin());
}
else
{
str = "None";
}
2020-05-12 12:19:05 +00:00
SetDlgItemText(IDC_ASSIGNED_MENU_ITEM, str.ToUTF16().c_str());
2016-01-27 09:11:59 +00:00
}
LRESULT COptionsShortCutsPage::OnMenuItemChanged(LPNMHDR lpnmh)
{
RefreshShortCutOptions(((LPNMTREEVIEW)lpnmh)->itemNew.hItem);
return true;
}
void COptionsShortCutsPage::RefreshShortCutOptions(HTREEITEM hItem)
{
HTREEITEM hParent = m_MenuItems.GetParentItem(hItem);
2021-04-12 11:35:39 +00:00
if (hParent == nullptr)
2016-01-27 09:11:59 +00:00
{
return;
}
2017-04-29 05:55:30 +00:00
RUNNING_STATE RunningState = (RUNNING_STATE)m_CpuState.GetItemData(m_CpuState.GetCurSel());
2016-01-27 09:11:59 +00:00
CShortCutItem * ShortCut = (CShortCutItem *)m_MenuItems.GetItemData(hItem);
m_CurrentKeys.ResetContent();
const SHORTCUT_KEY_LIST & ShortCutList = ShortCut->GetAccelItems();
for (SHORTCUT_KEY_LIST::const_iterator ShortCut_item = ShortCutList.begin(); ShortCut_item != ShortCutList.end(); ShortCut_item++)
{
2017-04-29 05:55:30 +00:00
if (ShortCut_item->Inactive() || !ShortCut_item->Active(RunningState))
2016-01-27 09:11:59 +00:00
{
continue;
}
2020-05-12 12:19:05 +00:00
const std::wstring & Name = ShortCut_item->Name();
2022-09-21 05:16:07 +00:00
m_CurrentKeys.SetItemData(m_CurrentKeys.AddString(Name.c_str()), (DWORD_PTR) & *ShortCut_item);
2016-01-27 09:11:59 +00:00
}
}
BOOL CALLBACK KeyPromptDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM /*lParam*/)
{
switch (uMsg)
{
case WM_INITDIALOG:
break;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDCANCEL:
SetForegroundWindow(GetParent(hDlg));
DestroyWindow(hDlg);
break;
}
break;
default:
return FALSE;
}
return TRUE;
}
void COptionsShortCutsPage::InputGetKeys(void)
{
2021-04-12 11:35:39 +00:00
HWND hKeyDlg = CreateDialogParam(GetModuleHandle(nullptr), MAKEINTRESOURCE(IDD_Key_Prompt), m_hWnd, (DLGPROC)KeyPromptDlgProc, (LPARAM)::GetDlgItem(m_hWnd, IDC_VIRTUALKEY));
2016-01-27 09:11:59 +00:00
::EnableWindow(GetParent(), false);
MSG msg;
2022-09-21 05:16:07 +00:00
for (bool fDone = false; !fDone; MsgWaitForMultipleObjects(0, nullptr, false, 45, QS_ALLINPUT))
{
while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
{
2016-01-27 09:11:59 +00:00
fDone = true;
2021-04-12 11:35:39 +00:00
::PostMessage(nullptr, WM_QUIT, 0, 0);
2016-01-27 09:11:59 +00:00
break;
}
2022-09-21 05:16:07 +00:00
if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
{
2016-01-27 09:11:59 +00:00
int nVirtKey = (int)msg.wParam;
2022-09-21 05:16:07 +00:00
if (nVirtKey == VK_SHIFT)
{
continue;
}
if (nVirtKey == VK_CONTROL)
{
continue;
}
if (nVirtKey == VK_MENU)
{
continue;
}
2016-01-27 09:11:59 +00:00
SendDlgItemMessage(IDC_VIRTUALKEY, CB_SETCURSEL, (WPARAM)-1, 0);
2022-09-21 05:16:07 +00:00
for (int count = 0; count < SendDlgItemMessage(IDC_VIRTUALKEY, CB_GETCOUNT, 0, 0); count++)
{
2016-01-27 09:11:59 +00:00
int Data = (int)SendDlgItemMessage(IDC_VIRTUALKEY, CB_GETITEMDATA, count, 0);
2022-09-21 05:16:07 +00:00
if (Data != nVirtKey)
{
continue;
}
2016-01-27 09:11:59 +00:00
SendDlgItemMessage(IDC_VIRTUALKEY, CB_SETCURSEL, count, 0);
SendDlgItemMessage(IDC_CTL, BM_SETCHECK, (GetKeyState(VK_CONTROL) & 0x80) != 0 ? BST_CHECKED : BST_UNCHECKED, 0);
SendDlgItemMessage(IDC_ALT, BM_SETCHECK, (GetKeyState(VK_MENU) & 0x80) != 0 ? BST_CHECKED : BST_UNCHECKED, 0);
SendDlgItemMessage(IDC_SHIFT, BM_SETCHECK, (GetKeyState(VK_SHIFT) & 0x80) != 0 ? BST_CHECKED : BST_UNCHECKED, 0);
SendMessage(WM_COMMAND, MAKELPARAM(IDC_VIRTUALKEY, LBN_SELCHANGE), (LPARAM)::GetDlgItem(m_hWnd, IDC_VIRTUALKEY));
SetForegroundWindow(GetParent());
::DestroyWindow(hKeyDlg);
}
continue;
}
2022-09-21 05:16:07 +00:00
if (!::IsDialogMessage(hKeyDlg, &msg))
{
2016-01-27 09:11:59 +00:00
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
2022-09-21 05:16:07 +00:00
if (!::IsWindow(hKeyDlg))
{
fDone = true;
}
2016-01-27 09:11:59 +00:00
}
::SetFocus(GetParent());
::EnableWindow(GetParent(), true);
}
void COptionsShortCutsPage::HidePage()
{
ShowWindow(SW_HIDE);
}
void COptionsShortCutsPage::ShowPage()
{
ShowWindow(SW_SHOW);
}
void COptionsShortCutsPage::ApplySettings(bool /*UpdateScreen*/)
{
m_ShortCuts.Save();
UISettingsSaveBool(Info_ShortCutsChanged, true);
2016-01-27 09:11:59 +00:00
}
bool COptionsShortCutsPage::EnableReset(void)
{
return m_EnableReset;
}
void COptionsShortCutsPage::ResetPage()
{
m_EnableReset = false;
m_ShortCuts.Load(true);
OnCpuStateChanged(LBN_SELCHANGE, IDC_C_CPU_STATE, GetDlgItem(IDC_C_CPU_STATE));
SendMessage(GetParent(), PSM_CHANGED, (WPARAM)m_hWnd, 0);
m_CurrentKeys.ResetContent();
CSettingsPageImpl<COptionsShortCutsPage>::ResetPage();
}