diff src/win32/AccelEditor.cpp @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/win32/AccelEditor.cpp	Sat Mar 03 10:31:27 2012 -0600
     1.3 @@ -0,0 +1,659 @@
     1.4 +// AccelEditor.cpp : implementation file
     1.5 +//
     1.6 +
     1.7 +#include "stdafx.h"
     1.8 +#include "resource.h"
     1.9 +#include "AccelEditor.h"
    1.10 +#include "CmdAccelOb.h"
    1.11 +#include "VBA.h"
    1.12 +
    1.13 +/////////////////////////////////////////////////////////////////////////////
    1.14 +// AccelEditor dialog
    1.15 +
    1.16 +AccelEditor::AccelEditor(CWnd *pParent, CMenu *pMenu, CAcceleratorManager *pExtMgr)
    1.17 +	: ResizeDlg(AccelEditor::IDD, pParent), m_pMenuSrc(pMenu), m_pExtMgr(pExtMgr)
    1.18 +{
    1.19 +	//{{AFX_DATA_INIT(AccelEditor)
    1.20 +	// NOTE: the ClassWizard will add member initialization here
    1.21 +	//}}AFX_DATA_INIT
    1.22 +}
    1.23 +
    1.24 +BOOL AccelEditor::IsModified() const
    1.25 +{
    1.26 +	return m_modified;
    1.27 +}
    1.28 +
    1.29 +const CAcceleratorManager &AccelEditor::GetResultMangager() const
    1.30 +{
    1.31 +	return m_result;
    1.32 +}
    1.33 +
    1.34 +void AccelEditor::DoDataExchange(CDataExchange *pDX)
    1.35 +{
    1.36 +	CDialog::DoDataExchange(pDX);
    1.37 +	//{{AFX_DATA_MAP(AccelEditor)
    1.38 +	DDX_Control(pDX, IDC_CURRENTS, m_currents);
    1.39 +	DDX_Control(pDX, IDC_ALREADY_AFFECTED, m_alreadyAffected);
    1.40 +	DDX_Control(pDX, IDC_COMMANDS, m_commands);
    1.41 +	DDX_Control(pDX, IDC_EDIT_KEY, m_key);
    1.42 +	DDX_Control(pDX, IDC_ACCELEDIT_AUTOTIMEOUT, m_timeout);
    1.43 +	DDX_Control(pDX, IDC_ACCELEDIT_PROGRESSBAR, m_progress);
    1.44 +	//}}AFX_DATA_MAP
    1.45 +}
    1.46 +
    1.47 +BEGIN_MESSAGE_MAP(AccelEditor, CDialog)
    1.48 +//{{AFX_MSG_MAP(AccelEditor)
    1.49 +ON_BN_CLICKED(ID_OK, &AccelEditor::OnOk)
    1.50 +ON_BN_CLICKED(ID_CANCEL, &AccelEditor::OnCancel)
    1.51 +ON_BN_CLICKED(IDC_ACCELEDIT_APPLY, &AccelEditor::OnApply)
    1.52 +ON_BN_CLICKED(IDC_RESET, &AccelEditor::OnReset)
    1.53 +ON_BN_CLICKED(IDC_ASSIGN, &AccelEditor::OnAssign)
    1.54 +ON_BN_CLICKED(IDC_REMOVE, &AccelEditor::OnRemove)
    1.55 +ON_BN_CLICKED(IDC_ACCELEDIT_REPLACE, &AccelEditor::OnReplace)
    1.56 +ON_CONTROL(EN_CHANGE, IDC_EDIT_KEY, &AccelEditor::OnKeyboardEditChange)
    1.57 +ON_CONTROL(EN_KILLFOCUS, IDC_EDIT_KEY, &AccelEditor::OnKeyboardEditKillfocus)
    1.58 +ON_CONTROL(EN_SETFOCUS, IDC_ACCELEDIT_AUTOTIMEOUT, &AccelEditor::OnTimeoutEditSetfocus)
    1.59 +ON_CONTROL(EN_KILLFOCUS, IDC_ACCELEDIT_AUTOTIMEOUT, &AccelEditor::OnTimeoutEditKillfocus)
    1.60 +ON_NOTIFY(TVN_SELCHANGED, IDC_COMMANDS, &AccelEditor::OnTvnSelchangedCommands)
    1.61 +//ON_NOTIFY(LVN_ITEMCHANGED, IDC_CURRENTS, &AccelEditor::OnListItemChanged)
    1.62 +ON_NOTIFY(NM_DBLCLK, IDC_CURRENTS, &AccelEditor::OnListDblClick)
    1.63 +ON_NOTIFY(NM_CLICK, IDC_CURRENTS, &AccelEditor::OnListClick)
    1.64 +ON_WM_TIMER()
    1.65 +//}}AFX_MSG_MAP
    1.66 +END_MESSAGE_MAP()
    1.67 +
    1.68 +/////////////////////////////////////////////////////////////////////////////
    1.69 +// AccelEditor message handlers
    1.70 +
    1.71 +BOOL AccelEditor::OnInitDialog()
    1.72 +{
    1.73 +	CDialog::OnInitDialog();
    1.74 +
    1.75 +	DIALOG_SIZER_START(sz)
    1.76 +	DIALOG_SIZER_ENTRY(ID_OK, DS_MoveX)
    1.77 +	DIALOG_SIZER_ENTRY(ID_CANCEL, DS_MoveX)
    1.78 +	DIALOG_SIZER_ENTRY(IDC_ACCELEDIT_APPLY, DS_MoveX)
    1.79 +	DIALOG_SIZER_ENTRY(IDC_ASSIGN, DS_MoveX)
    1.80 +	DIALOG_SIZER_ENTRY(IDC_REMOVE, DS_MoveX)
    1.81 +	DIALOG_SIZER_ENTRY(IDC_ACCELEDIT_REPLACE, DS_MoveX)
    1.82 +	DIALOG_SIZER_ENTRY(IDC_COMMANDS, DS_SizeX | DS_SizeY)
    1.83 +	DIALOG_SIZER_ENTRY(IDC_CURRENTS, DS_MoveX | DS_SizeY)
    1.84 +	DIALOG_SIZER_ENTRY(IDC_EDIT_KEY, DS_MoveX | DS_MoveY)
    1.85 +	DIALOG_SIZER_ENTRY(IDC_EDIT_KEY, DS_MoveX | DS_MoveY)
    1.86 +	DIALOG_SIZER_ENTRY(IDC_STATIC2, DS_MoveY)
    1.87 +	DIALOG_SIZER_ENTRY(IDC_STATIC3, DS_MoveX | DS_MoveY)
    1.88 +	DIALOG_SIZER_ENTRY(IDC_STATIC4, DS_MoveX | DS_MoveY)
    1.89 +	DIALOG_SIZER_ENTRY(IDC_STATIC5, DS_MoveX | DS_MoveY)
    1.90 +	DIALOG_SIZER_ENTRY(IDC_ACCELEDIT_AUTOTIMEOUT, DS_SizeX | DS_MoveY)
    1.91 +	DIALOG_SIZER_END()
    1.92 +	SetData(sz,
    1.93 +	        TRUE,
    1.94 +	        HKEY_CURRENT_USER,
    1.95 +	        "Software\\Emulators\\VisualBoyAdvance\\Viewer\\AccelEditor",
    1.96 +	        NULL);
    1.97 +
    1.98 +	if (m_pExtMgr)
    1.99 +		m_result = m_mgr = *m_pExtMgr;
   1.100 +
   1.101 +	m_currents.SetExtendedStyle(LVS_EX_FULLROWSELECT);
   1.102 +	m_currents.InsertColumn(0, "Keys");
   1.103 +	m_currents.SetColumnWidth(0, LVSCW_AUTOSIZE_USEHEADER);
   1.104 +	InitCommands();
   1.105 +	m_autoMode	   = AUTO_REPLACE;
   1.106 +	m_modified	   = FALSE;
   1.107 +	m_timeoutValue = 1000;
   1.108 +	CString timeoutStr;
   1.109 +	timeoutStr.Format("%d", m_timeoutValue);
   1.110 +	m_timeout.SetWindowText(timeoutStr);
   1.111 +	m_progress.SetPos(0);
   1.112 +
   1.113 +	GetDlgItem(IDC_ACCELEDIT_APPLY)->EnableWindow(FALSE);
   1.114 +	return TRUE; // return TRUE unless you set the focus to a control
   1.115 +	             // EXCEPTION: OCX Property Pages should return FALSE
   1.116 +}
   1.117 +
   1.118 +void AccelEditor::AddCommandsFromTable()
   1.119 +{
   1.120 +	POSITION pos = m_mgr.m_mapAccelString.GetStartPosition();
   1.121 +	while (pos != NULL)
   1.122 +	{
   1.123 +		CString command;
   1.124 +		WORD	wID;
   1.125 +		m_mgr.m_mapAccelString.GetNextAssoc(pos, command, wID);
   1.126 +		int nPos = command.Find('\\');
   1.127 +
   1.128 +		if (nPos == 0)  // skip menu commands
   1.129 +		{
   1.130 +			continue;
   1.131 +		}
   1.132 +
   1.133 +		HTREEITEM newItem = TVI_ROOT;
   1.134 +#if 0
   1.135 +/*
   1.136 +        while (nPos != -1)
   1.137 +        {
   1.138 +            newItem = m_commands.InsertItem(command.Left(nPos), newItem);
   1.139 +            command.Delete(0, nPos + 1);
   1.140 +            nPos = command.Find('\\');
   1.141 +        }
   1.142 + */
   1.143 +#endif
   1.144 +		newItem = m_commands.InsertItem(command, newItem);
   1.145 +		m_commands.SetItemData(newItem, wID);
   1.146 +		m_hItems.AddTail(newItem);
   1.147 +	}
   1.148 +}
   1.149 +
   1.150 +// recursive calls
   1.151 +void AccelEditor::AddCommandsFromMenu(CMenu *pMenu, HTREEITEM hParent)
   1.152 +{
   1.153 +	UINT nIndexMax = pMenu->GetMenuItemCount();
   1.154 +	for (UINT nIndex = 0; nIndex < nIndexMax; ++nIndex)
   1.155 +	{
   1.156 +		UINT nID = pMenu->GetMenuItemID(nIndex);
   1.157 +		if (nID == 0)
   1.158 +			continue;  // menu separator or invalid cmd - ignore it
   1.159 +
   1.160 +		if (nID == (UINT)-1)
   1.161 +		{
   1.162 +			// possibly a submenu
   1.163 +			CMenu *pSubMenu = pMenu->GetSubMenu(nIndex);
   1.164 +			if (pSubMenu != NULL)
   1.165 +			{
   1.166 +				CString tempStr;
   1.167 +				pMenu->GetMenuString(nIndex, tempStr, MF_BYPOSITION);
   1.168 +				tempStr.Remove('&');
   1.169 +				HTREEITEM newItem = m_commands.InsertItem(tempStr, hParent);
   1.170 +				AddCommandsFromMenu(pSubMenu, newItem);
   1.171 +			}
   1.172 +		}
   1.173 +		else
   1.174 +		{
   1.175 +			// normal menu item
   1.176 +			// generate the strings
   1.177 +			CString command;
   1.178 +			pMenu->GetMenuString(nIndex, command, MF_BYPOSITION);
   1.179 +			int nPos = command.ReverseFind('\t');
   1.180 +			if (nPos != -1)
   1.181 +			{
   1.182 +				command.Delete(nPos, command.GetLength() - nPos);
   1.183 +			}
   1.184 +			command.Remove('&');
   1.185 +			HTREEITEM newItem = m_commands.InsertItem(command, hParent);
   1.186 +			m_commands.SetItemData(newItem, nID);
   1.187 +			m_hItems.AddTail(newItem);
   1.188 +		}
   1.189 +	}
   1.190 +}
   1.191 +
   1.192 +void AccelEditor::InitCommands()
   1.193 +{
   1.194 +	m_commands.DeleteAllItems();
   1.195 +	m_hItems.RemoveAll();
   1.196 +	m_alreadyAffected.SetWindowText("");
   1.197 +
   1.198 +	AddCommandsFromMenu(m_pMenuSrc, TVI_ROOT);
   1.199 +	AddCommandsFromTable();
   1.200 +}
   1.201 +
   1.202 +BOOL AccelEditor::PreTranslateMessage(MSG *pMsg)
   1.203 +{
   1.204 +	CWnd *pFocus = GetFocus();
   1.205 +	if (pFocus == &m_currents)
   1.206 +	{
   1.207 +		if (pMsg->message == WM_KEYDOWN)
   1.208 +		{
   1.209 +			switch (pMsg->wParam)
   1.210 +			{
   1.211 +			case VK_ESCAPE:
   1.212 +				m_currents.SetItemState(-1, 0, LVIS_SELECTED);
   1.213 +				CheckListSelections();
   1.214 +				break;
   1.215 +			case VK_RETURN:
   1.216 +			case VK_INSERT:
   1.217 +				// kludge to workaround CKeyboardEdit::PreTranslateMessage()
   1.218 +				break;
   1.219 +			case VK_DELETE:
   1.220 +			case VK_BACK:
   1.221 +				OnRemove();
   1.222 +				break;
   1.223 +			case VK_F6:
   1.224 +			case VK_LEFT:
   1.225 +				m_commands.SetFocus();
   1.226 +				break;
   1.227 +			case VK_RIGHT:
   1.228 +				GetDlgItem(ID_OK)->SetFocus();
   1.229 +			default:
   1.230 +				return ResizeDlg::PreTranslateMessage(pMsg);
   1.231 +			}
   1.232 +			return TRUE;
   1.233 +		}
   1.234 +		else if (pMsg->message == WM_KEYUP)	// kludge to workaround CKeyboardEdit::PreTranslateMessage()
   1.235 +		{
   1.236 +			switch (pMsg->wParam)
   1.237 +			{
   1.238 +			case VK_RETURN:
   1.239 +				OnEdit();
   1.240 +				break;
   1.241 +			case VK_INSERT:
   1.242 +				OnNew();
   1.243 +				break;
   1.244 +			default:
   1.245 +				return ResizeDlg::PreTranslateMessage(pMsg);
   1.246 +			}
   1.247 +			return TRUE;
   1.248 +		}
   1.249 +	}
   1.250 +	else if (pFocus == &m_commands)
   1.251 +	{
   1.252 +		if (pMsg->message == WM_KEYDOWN)
   1.253 +		{
   1.254 +			switch (pMsg->wParam)
   1.255 +			{
   1.256 +			case VK_F6:
   1.257 +				m_currents.SetFocus();
   1.258 +				break;
   1.259 +			case VK_RIGHT:
   1.260 +				if (!m_commands.ItemHasChildren(m_commands.GetSelectedItem()))
   1.261 +				{
   1.262 +					m_currents.SetFocus();
   1.263 +					break;
   1.264 +				}
   1.265 +				// fall through
   1.266 +			default:
   1.267 +				return ResizeDlg::PreTranslateMessage(pMsg);
   1.268 +			}
   1.269 +			return TRUE;
   1.270 +		}
   1.271 +	}
   1.272 +
   1.273 +	return ResizeDlg::PreTranslateMessage(pMsg);
   1.274 +}
   1.275 +
   1.276 +void AccelEditor::OnOk()
   1.277 +{
   1.278 +	OnApply();
   1.279 +//	OnTimeoutEditKillfocus();
   1.280 +	EndDialog(TRUE);
   1.281 +}
   1.282 +
   1.283 +void AccelEditor::OnCancel()
   1.284 +{
   1.285 +//	OnTimeoutEditKillfocus();
   1.286 +//	EndDialog(m_modified);
   1.287 +	EndDialog(FALSE);	// this allows the caller to cancel even if the user has Apply'ed
   1.288 +}
   1.289 +
   1.290 +void AccelEditor::OnApply()
   1.291 +{
   1.292 +	m_result   = m_mgr;
   1.293 +	GetDlgItem(IDC_ACCELEDIT_APPLY)->EnableWindow(FALSE);
   1.294 +}
   1.295 +
   1.296 +void AccelEditor::OnReset()
   1.297 +{
   1.298 +	m_mgr.Default(); /// FIXME accelerator reset NYI
   1.299 +	systemMessage(
   1.300 +	    0,
   1.301 +	    "The \"Reset All Accelerators\" feature is currently unimplemented.\nYou can achieve the same result by closing VBA, opening up your \"vba.ini\" file, deleting the line that starts with \"keyboard\", then reopening VBA.");
   1.302 +	InitCommands(); // update the listboxes.
   1.303 +}
   1.304 +
   1.305 +void AccelEditor::OnAssign()
   1.306 +{
   1.307 +	if (CheckAffected())
   1.308 +		return;
   1.309 +
   1.310 +	// get the currently selected group
   1.311 +	HTREEITEM hItem = m_commands.GetSelectedItem();
   1.312 +	if (hItem == NULL)
   1.313 +		return;  // abort
   1.314 +
   1.315 +	// Get the object who manage the accels list, associated to the command.
   1.316 +	WORD wIDCommand = LOWORD(m_commands.GetItemData(hItem));
   1.317 +
   1.318 +	CCmdAccelOb *pCmdAccel;
   1.319 +	if (m_mgr.m_mapAccelTable.Lookup(wIDCommand, pCmdAccel) != TRUE)
   1.320 +		return;
   1.321 +
   1.322 +	WORD wKey;
   1.323 +	bool bCtrl, bAlt, bShift;
   1.324 +	if (!m_key.GetAccelKey(wKey, bCtrl, bAlt, bShift))
   1.325 +		return;  // no valid key, abort
   1.326 +
   1.327 +	BYTE cVirt = 0;
   1.328 +	if (bCtrl)
   1.329 +		cVirt |= FCONTROL;
   1.330 +	if (bAlt)
   1.331 +		cVirt |= FALT;
   1.332 +	if (bShift)
   1.333 +		cVirt |= FSHIFT;
   1.334 +
   1.335 +	cVirt |= FVIRTKEY;
   1.336 +
   1.337 +	// Create the new key...
   1.338 +	CAccelsOb *pAccel = new CAccelsOb(cVirt, wKey, false);
   1.339 +	ASSERT(pAccel != NULL);
   1.340 +	// ...and add in the list.
   1.341 +	pCmdAccel->m_Accels.AddTail(pAccel);
   1.342 +
   1.343 +	// Update the listbox.
   1.344 +	CString szBuffer;
   1.345 +	pAccel->GetString(szBuffer);
   1.346 +
   1.347 +	int index = m_currents.GetNextItem(-1, LVNI_SELECTED);
   1.348 +	if (index < 0)
   1.349 +		index = 0;
   1.350 +	m_currents.InsertItem(index, szBuffer);
   1.351 +	m_currents.SetItemData(index, reinterpret_cast<DWORD>(pAccel));
   1.352 +	m_currents.SetItemState(-1, 0, LVIS_SELECTED);	// deselect other items first
   1.353 +	m_currents.SetItemState(index, LVIS_FOCUSED | LVIS_SELECTED, LVIS_FOCUSED | LVIS_SELECTED);
   1.354 +	GetDlgItem(IDC_REMOVE)->EnableWindow(TRUE);
   1.355 +	GetDlgItem(IDC_ACCELEDIT_REPLACE)->EnableWindow(TRUE);
   1.356 +
   1.357 +	// Reset the key editor.
   1.358 +//	m_key.ResetKey();
   1.359 +
   1.360 +	m_modified = TRUE;
   1.361 +	GetDlgItem(IDC_ACCELEDIT_APPLY)->EnableWindow(TRUE);
   1.362 +}
   1.363 +
   1.364 +void AccelEditor::OnRemove()
   1.365 +{
   1.366 +	// Some controls
   1.367 +	POSITION selected = m_currents.GetFirstSelectedItemPosition();
   1.368 +	if (selected == NULL)
   1.369 +		return;
   1.370 +
   1.371 +	HTREEITEM hItem = m_commands.GetSelectedItem();
   1.372 +	if (hItem == NULL)
   1.373 +		return;
   1.374 +
   1.375 +	// Ref to the ID command
   1.376 +	WORD wIDCommand = LOWORD(m_commands.GetItemData(hItem));
   1.377 +
   1.378 +	// Run through the accels, and control if it can be deleted.
   1.379 +	CCmdAccelOb *pCmdAccel;
   1.380 +	if (m_mgr.m_mapAccelTable.Lookup(wIDCommand, pCmdAccel) == TRUE)
   1.381 +	{
   1.382 +		POSITION pos = pCmdAccel->m_Accels.GetHeadPosition();
   1.383 +		POSITION PrevPos;
   1.384 +		while (pos != NULL)
   1.385 +		{
   1.386 +			PrevPos = pos;
   1.387 +			CAccelsOb *pAccel = pCmdAccel->m_Accels.GetNext(pos);
   1.388 +			do
   1.389 +			{
   1.390 +				int indexCurrent = m_currents.GetNextSelectedItem(selected);
   1.391 +				CAccelsOb *pAccelCurrent = reinterpret_cast<CAccelsOb *>(m_currents.GetItemData(indexCurrent));
   1.392 +				if (pAccel == pAccelCurrent)
   1.393 +				{
   1.394 +					if (!pAccel->m_bLocked)
   1.395 +					{
   1.396 +						// not locked, so we delete the key
   1.397 +						pCmdAccel->m_Accels.RemoveAt(PrevPos);
   1.398 +						delete pAccel;
   1.399 +						// and update the listboxes/key editor/static text
   1.400 +						m_currents.DeleteItem(indexCurrent);
   1.401 +						m_modified = TRUE;
   1.402 +						break;
   1.403 +					}
   1.404 +					else
   1.405 +					{
   1.406 +						systemMessage(0, "Unable to remove this locked accelerator: ", m_currents.GetItemText(indexCurrent, KEY_COLUMN));
   1.407 +						m_currents.SetItemState(indexCurrent, 0, LVIS_SELECTED); // deselect it
   1.408 +						break;
   1.409 +					}
   1.410 +				}
   1.411 +			}
   1.412 +			while (selected != NULL);
   1.413 +
   1.414 +			selected = m_currents.GetFirstSelectedItemPosition();
   1.415 +			if (selected == NULL)	// the normal exit of this function
   1.416 +			{
   1.417 +				m_currents.SetItemState(m_currents.GetNextItem(-1, LVIS_FOCUSED), LVIS_SELECTED, LVIS_SELECTED);
   1.418 +				if (m_currents.GetSelectedCount() == 0)
   1.419 +				{
   1.420 +					GetDlgItem(IDC_REMOVE)->EnableWindow(FALSE);
   1.421 +					GetDlgItem(IDC_ACCELEDIT_REPLACE)->EnableWindow(FALSE);
   1.422 +				}
   1.423 +				GetDlgItem(IDC_ACCELEDIT_APPLY)->EnableWindow(m_modified);
   1.424 +				return;
   1.425 +			}
   1.426 +		}
   1.427 +		systemMessage(0, "internal error (AccelEditor::Remove : pAccel unavailable)");
   1.428 +		return;
   1.429 +	}
   1.430 +	systemMessage(0, "internal error (AccelEditor::Remove : Lookup failed)");
   1.431 +}
   1.432 +
   1.433 +void AccelEditor::OnReplace()
   1.434 +{
   1.435 +	if (CheckAffected())
   1.436 +		return;
   1.437 +	OnRemove();
   1.438 +	OnAssign();
   1.439 +}
   1.440 +
   1.441 +void AccelEditor::OnNew()
   1.442 +{
   1.443 +	m_autoMode = AUTO_NEW;
   1.444 +	m_key.SetFocus();
   1.445 +}
   1.446 +
   1.447 +void AccelEditor::OnEdit()
   1.448 +{
   1.449 +	m_autoMode = AUTO_REPLACE;
   1.450 +	m_key.SetFocus();
   1.451 +}
   1.452 +
   1.453 +BOOL AccelEditor::CheckAffected()
   1.454 +{
   1.455 +	m_alreadyAffected.SetWindowText("");
   1.456 +
   1.457 +	WORD wKey;
   1.458 +	bool bCtrl, bAlt, bShift;
   1.459 +	if (!m_key.GetAccelKey(wKey, bCtrl, bAlt, bShift))
   1.460 +		return TRUE;  // no valid key, abort
   1.461 +
   1.462 +	POSITION posItem = m_hItems.GetHeadPosition();
   1.463 +	while (posItem != NULL)
   1.464 +	{
   1.465 +		HTREEITEM hItem		  = m_hItems.GetNext(posItem);
   1.466 +		WORD	  wIDCommand2 = LOWORD(m_commands.GetItemData(hItem));
   1.467 +
   1.468 +		CCmdAccelOb *pCmdAccel;
   1.469 +		m_mgr.m_mapAccelTable.Lookup(wIDCommand2, pCmdAccel);
   1.470 +
   1.471 +		POSITION pos = pCmdAccel->m_Accels.GetHeadPosition();
   1.472 +		while (pos != NULL)
   1.473 +		{
   1.474 +			CAccelsOb *pAccel = pCmdAccel->m_Accels.GetNext(pos);
   1.475 +			if (pAccel->IsEqual(wKey, bCtrl, bAlt, bShift))
   1.476 +			{
   1.477 +				// the key is already affected (in the same or other command)
   1.478 +				// (the parts that were commented out allow for a one-to-many mapping,
   1.479 +				//  which is only disabled because the MFC stuff that automagically activates the commands
   1.480 +				//  doesn't seem to be capable of activating more than one command per accelerator...)
   1.481 +				m_alreadyAffected.SetWindowText(pCmdAccel->m_szCommand);
   1.482 +				return TRUE;
   1.483 +			}
   1.484 +		}
   1.485 +	}
   1.486 +
   1.487 +	return FALSE;
   1.488 +}
   1.489 +
   1.490 +BOOL AccelEditor::CheckJammed()
   1.491 +{
   1.492 +	WORD jam;
   1.493 +	if (m_key.GetJamKey(jam))
   1.494 +	{
   1.495 +		// these go first, or the timer would be set again
   1.496 +		m_key.ResetKey();
   1.497 +		m_key.SetWindowText("Interrupted");
   1.498 +		if (m_currents.IsWindowEnabled())
   1.499 +			m_currents.SetFocus();
   1.500 +		else
   1.501 +			m_commands.SetFocus();
   1.502 +		return TRUE;
   1.503 +	}
   1.504 +	return FALSE;
   1.505 +}
   1.506 +
   1.507 +BOOL AccelEditor::CheckListSelections()
   1.508 +{
   1.509 +	BOOL result = m_currents.GetFirstSelectedItemPosition() ? TRUE : FALSE;
   1.510 +
   1.511 +	GetDlgItem(IDC_REMOVE)->EnableWindow(result);
   1.512 +	GetDlgItem(IDC_ACCELEDIT_REPLACE)->EnableWindow(result);
   1.513 +
   1.514 +	return result;
   1.515 +}
   1.516 +
   1.517 +void AccelEditor::OnTvnSelchangedCommands(NMHDR *pNMHDR, LRESULT *pResult)
   1.518 +{
   1.519 +//	LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR);
   1.520 +
   1.521 +	// TODO: Add your control notification handler code here
   1.522 +	// Check if some commands exist.
   1.523 +	HTREEITEM hItem = m_commands.GetSelectedItem();
   1.524 +	if (hItem == NULL)
   1.525 +		return;
   1.526 +
   1.527 +	m_currents.DeleteAllItems();
   1.528 +
   1.529 +	WORD wIDCommand = LOWORD(m_commands.GetItemData(hItem));
   1.530 +	CCmdAccelOb *pCmdAccel;
   1.531 +	if (m_mgr.m_mapAccelTable.Lookup(wIDCommand, pCmdAccel))
   1.532 +	{
   1.533 +		CAccelsOb *pAccel;
   1.534 +		CString	   szBuffer;
   1.535 +		POSITION   pos = pCmdAccel->m_Accels.GetHeadPosition();
   1.536 +
   1.537 +		// Add the keys to the 'currents keys' listbox.
   1.538 +		while (pos != NULL)
   1.539 +		{
   1.540 +			pAccel = pCmdAccel->m_Accels.GetNext(pos);
   1.541 +			pAccel->GetString(szBuffer);
   1.542 +			int index = m_currents.InsertItem(m_currents.GetItemCount(), szBuffer);
   1.543 +			// and a pointer to the accel object.
   1.544 +			m_currents.SetItemData(index, (DWORD)pAccel);
   1.545 +		}
   1.546 +
   1.547 +		m_currents.SetItemState(0, LVIS_FOCUSED | LVIS_SELECTED, LVIS_FOCUSED | LVIS_SELECTED);
   1.548 +		GetDlgItem(IDC_ASSIGN)->EnableWindow(TRUE);
   1.549 +		m_currents.EnableWindow(TRUE);
   1.550 +	}
   1.551 +	else
   1.552 +	{
   1.553 +		GetDlgItem(IDC_ASSIGN)->EnableWindow(FALSE);
   1.554 +		m_currents.EnableWindow(FALSE);
   1.555 +	}
   1.556 +
   1.557 +	// Init the key editor
   1.558 +//  m_pKey->ResetKey();
   1.559 +//	m_alreadyAffected.SetWindowText("");
   1.560 +
   1.561 +	CheckListSelections();
   1.562 +
   1.563 +	*pResult = 0;
   1.564 +}
   1.565 +
   1.566 +/*
   1.567 +void AccelEditor::OnListItemChanged(NMHDR *pNMHDR, LRESULT *pResult)
   1.568 +{
   1.569 +	NMLISTVIEW *pNMListView = reinterpret_cast<NMLISTVIEW *>(pNMHDR);
   1.570 +	if (pNMListView->uChanged == LVIF_STATE)
   1.571 +	{
   1.572 +		if ((pNMListView->uOldState & LVIS_SELECTED) && !(pNMListView->uNewState & LVIS_SELECTED))
   1.573 +		{
   1.574 +		}
   1.575 +	}
   1.576 +
   1.577 +	*pResult = 0;
   1.578 +}
   1.579 +*/
   1.580 +
   1.581 +void AccelEditor::OnListClick(NMHDR *pNMHDR, LRESULT *pResult)
   1.582 +{
   1.583 +	CheckListSelections();
   1.584 +	*pResult = 0;
   1.585 +}
   1.586 +
   1.587 +void AccelEditor::OnListDblClick(NMHDR *pNMHDR, LRESULT *pResult)
   1.588 +{
   1.589 +	if (m_currents.GetFirstSelectedItemPosition())
   1.590 +		OnEdit();
   1.591 +	else
   1.592 +		OnNew();
   1.593 +	*pResult = 0;
   1.594 +}
   1.595 +
   1.596 +void AccelEditor::OnKeyboardEditChange()
   1.597 +{
   1.598 +	if (!m_key.IsDefined())
   1.599 +		return;
   1.600 +
   1.601 +//	if (CheckJammed())
   1.602 +//		return;
   1.603 +
   1.604 +	OnKeyboardEditKillfocus();
   1.605 +	CheckAffected();
   1.606 +	if (m_timeoutValue == 0)
   1.607 +		return;
   1.608 +
   1.609 +	m_progress.SetRange32(0, m_timeoutValue);
   1.610 +	SetTimer(1, 50, NULL);
   1.611 +}
   1.612 +
   1.613 +void AccelEditor::OnKeyboardEditKillfocus()
   1.614 +{
   1.615 +	KillTimer(1);
   1.616 +	m_timer = 0;
   1.617 +	m_progress.SetPos(0);
   1.618 +	m_progress.SetBarColor(RGB(128, 0, 255));
   1.619 +}
   1.620 +
   1.621 +void AccelEditor::OnTimeoutEditSetfocus()
   1.622 +{
   1.623 +	m_timeout.PostMessage(EM_SETSEL, 0, -1);
   1.624 +}
   1.625 +
   1.626 +void AccelEditor::OnTimeoutEditKillfocus()
   1.627 +{
   1.628 +	CString str;
   1.629 +	m_timeout.GetWindowText(str);
   1.630 +	m_timeoutValue = atoi(str);
   1.631 +	m_autoMode	   = AUTO_REPLACE;
   1.632 +}
   1.633 +
   1.634 +void AccelEditor::OnTimer(UINT_PTR nIDEvent)
   1.635 +{
   1.636 +	if (nIDEvent == 1)
   1.637 +	{
   1.638 +		m_timer += 50;
   1.639 +		if (m_timer >= m_timeoutValue)
   1.640 +		{
   1.641 +			m_progress.SetPos(m_timeoutValue);
   1.642 +			m_progress.SetBarColor(RGB(255, 255, 0));
   1.643 +			if (m_autoMode == AUTO_NEW)
   1.644 +			{
   1.645 +				OnAssign();
   1.646 +			}
   1.647 +			else
   1.648 +			{
   1.649 +				OnReplace();
   1.650 +			}
   1.651 +			if (m_currents.IsWindowEnabled())
   1.652 +				m_currents.SetFocus();
   1.653 +			else
   1.654 +				m_commands.SetFocus();
   1.655 +			return;
   1.656 +		}
   1.657 +		UINT green = (m_timer * 255 / m_timeoutValue) ;
   1.658 +		m_progress.SetBarColor(RGB(128 + green / 2, green, 255 - green));
   1.659 +		m_progress.SetPos(m_timer);
   1.660 +	}
   1.661 +}
   1.662 +