示例#1
0
BOOL AccelEditor::CheckAffected()
{
	m_alreadyAffected.SetWindowText("");

	WORD wKey;
	bool bCtrl, bAlt, bShift;
	if (!m_key.GetAccelKey(wKey, bCtrl, bAlt, bShift))
		return TRUE;  // no valid key, abort

	POSITION posItem = m_hItems.GetHeadPosition();
	while (posItem != NULL)
	{
		HTREEITEM hItem		  = m_hItems.GetNext(posItem);
		WORD	  wIDCommand2 = LOWORD(m_commands.GetItemData(hItem));

		CCmdAccelOb *pCmdAccel;
		m_mgr.m_mapAccelTable.Lookup(wIDCommand2, pCmdAccel);

		POSITION pos = pCmdAccel->m_Accels.GetHeadPosition();
		while (pos != NULL)
		{
			CAccelsOb *pAccel = pCmdAccel->m_Accels.GetNext(pos);
			if (pAccel->IsEqual(wKey, bCtrl, bAlt, bShift))
			{
				// the key is already affected (in the same or other command)
				// (the parts that were commented out allow for a one-to-many mapping,
				//  which is only disabled because the MFC stuff that automagically activates the commands
				//  doesn't seem to be capable of activating more than one command per accelerator...)
				m_alreadyAffected.SetWindowText(pCmdAccel->m_szCommand);
				return TRUE;
			}
		}
	}

	return FALSE;
}
void AccelEditor::OnAssign()
{
  // Control if it's not already affected
  CCmdAccelOb* pCmdAccel;
  CAccelsOb* pAccel;
  WORD wIDCommand;
  POSITION pos;

  WORD wKey;
  bool bCtrl, bAlt, bShift;

  if (!m_key.GetAccelKey(wKey, bCtrl, bAlt, bShift))
    return; // no valid key, abort

  int count = m_commands.GetCount();
  int index;
  for (index = 0; index < count; index++) {

    wIDCommand = LOWORD(m_commands.GetItemData(index));
    mgr.m_mapAccelTable.Lookup(wIDCommand, pCmdAccel);

    pos = pCmdAccel->m_Accels.GetHeadPosition();
    while (pos != NULL) {
      pAccel = pCmdAccel->m_Accels.GetNext(pos);
      if (pAccel->IsEqual(wKey, bCtrl, bAlt, bShift)) {
        // the key is already affected (in the same or other command)
        m_alreadyAffected.SetWindowText(pCmdAccel->m_szCommand);
        m_key.SetSel(0, -1);
        return; // abort
      }
    }
  }

  // OK, we can add the accel key in the currently selected group
  index = m_commands.GetCurSel();
  if (index == LB_ERR)
    return;

  // Get the object who manage the accels list, associated to the command.
  wIDCommand = LOWORD(m_commands.GetItemData(index));

  if (mgr.m_mapAccelTable.Lookup(wIDCommand, pCmdAccel) != TRUE)
    return;

  BYTE cVirt = 0;
  if (bCtrl)
    cVirt |= FCONTROL;
  if (bAlt)
    cVirt |= FALT;
  if (bShift)
    cVirt |= FSHIFT;

  cVirt |= FVIRTKEY;

  // Create the new key...
  pAccel = new CAccelsOb(cVirt, wKey, false);
  ASSERT(pAccel != NULL);
  // ...and add in the list.
  pCmdAccel->m_Accels.AddTail(pAccel);

  // Update the listbox.
  CString szBuffer;
  pAccel->GetString(szBuffer);

  index = m_currents.AddString(szBuffer);
  m_currents.SetItemData(index, (DWORD_PTR)pAccel);

  // Reset the key editor.
  m_key.ResetKey();
}
void AccelEditor::Assign()
{
  // Control if it's not already affected
  CCmdAccelOb* pCmdAccel;
  CAccelsOb* pAccel;
  WORD wIDCommand;
  
  WORD wKey;
  bool bCtrl, bAlt, bShift;

  if (!m_Key.GetAccelKey(wKey, bCtrl, bAlt, bShift))
    return; // no valid key, abort

  HWND h = GetDlgItem(IDC_COMMANDS);

  int count = ::SendMessage(h, LB_GETCOUNT, 0, 0);
  for (int index = 0; index < count; index++) {

    wIDCommand = LOWORD(::SendMessage(h, LB_GETITEMDATA, index, 0));
    CMapWordToCCmdAccelOb::iterator it = mgr.m_mapAccelTable.find(wIDCommand);
    ASSERT(it != mgr.m_mapAccelTable.end());

    pCmdAccel = it->second;

    std::list<CAccelsOb*>::iterator it2 = pCmdAccel->m_Accels.begin();
    while (it2 != pCmdAccel->m_Accels.end()) {
      pAccel = *it2;
      it2++;
      if (pAccel->IsEqual(wKey, bCtrl, bAlt, bShift)) {
        // the key is already affected (in the same or other command)
        ::SetWindowText(GetDlgItem(IDC_ALREADY_AFFECTED), pCmdAccel->m_szCommand);
        ::SendMessage(m_Key, EM_SETSEL, 0, -1);
        return; // abort
      }
    }
  }

  // OK, we can add the accel key in the currently selected group
  index = ::SendMessage(h, LB_GETCURSEL, 0, 0);
  if (index == LB_ERR)
    return;

  // Get the object who manage the accels list, associated to the command.
  wIDCommand = LOWORD(::SendMessage(h, LB_GETITEMDATA, index, 0));

  CMapWordToCCmdAccelOb::iterator it = mgr.m_mapAccelTable.find(wIDCommand);

  if (it == mgr.m_mapAccelTable.end())
    return;

  pCmdAccel = it->second;
  
  BYTE cVirt = 0;
  if (bCtrl)
    cVirt |= FCONTROL;
  if (bAlt)
    cVirt |= FALT;
  if (bShift)
    cVirt |= FSHIFT;

  cVirt |= FVIRTKEY;

  // Create the new key...
  pAccel = new CAccelsOb(cVirt, wKey, false);
  ASSERT(pAccel != NULL);
  // ...and add in the list.
  pCmdAccel->m_Accels.push_back(pAccel);

  // Update the listbox.
  CStdString szBuffer;
  pAccel->GetString(szBuffer);
  h = GetDlgItem(IDC_CURRENTS);

  index = ::SendMessage(h, LB_ADDSTRING, 0, (LPARAM)((LPCSTR)szBuffer));
  ::SendMessage(h, LB_SETITEMDATA, index, (LPARAM)pAccel);

  // Reset the key editor.
  m_Key.ResetKey();

}