bool CAcceleratorManager::AddAccel(BYTE cVirt, WORD wIDCommand, WORD wKey, LPCTSTR szCommand, bool bLocked)
{
  ASSERT(szCommand != NULL);

  WORD wIDCmd;
  if (m_mapAccelString.Lookup(szCommand, wIDCmd) == TRUE) {
    if (wIDCmd != wIDCommand)
      return false;
  }

  CCmdAccelOb* pCmdAccel = NULL;
  if (m_mapAccelTable.Lookup(wIDCommand, pCmdAccel) == TRUE) {
    if (pCmdAccel->m_szCommand != szCommand) {
      return false;
    }
    CAccelsOb* pAccel;
    POSITION pos = pCmdAccel->m_Accels.GetHeadPosition();
    while (pos != NULL) {
      pAccel = pCmdAccel->m_Accels.GetNext(pos);
      if (pAccel->m_cVirt == cVirt &&
          pAccel->m_wKey == wKey)
        return FALSE;
    }
    // Adding the accelerator
    pCmdAccel->Add(cVirt, wKey, bLocked);

  } else {
    pCmdAccel = new CCmdAccelOb(cVirt, wIDCommand, wKey, szCommand, bLocked);
    ASSERT(pCmdAccel != NULL);
    m_mapAccelTable.SetAt(wIDCommand, pCmdAccel);
  }
  // 2nd table
  m_mapAccelString.SetAt(szCommand, wIDCommand);
  return true;
}
//////////////////////////////////////////////////////////////////////
// In/Out to the registry
//////////////////////////////////////////////////////////////////////
//
//
bool CAcceleratorManager::Load(HKEY hRegKey, LPCTSTR szRegKey)
{
  ASSERT(szRegKey != NULL);
  HKEY hKey;

  m_hRegKey = hRegKey;
  m_szRegKey = szRegKey;

  LONG res = RegOpenKeyEx(hRegKey, szRegKey, 0L, KEY_ALL_ACCESS, &hKey);
  if(res == ERROR_SUCCESS) {
    DWORD data[2048/sizeof(DWORD)];

    DWORD dwType;
    DWORD len = sizeof(data);
    res = RegQueryValueEx(hKey, "keyboard", NULL, &dwType, 
                          (LPBYTE)data, &len);
    RegCloseKey(hKey);

    if(res == ERROR_SUCCESS) {
      int count = len/sizeof(DWORD);

      CCmdAccelOb* pCmdAccel;
      CAccelsOb* pAccel;
      DWORD dwIDAccelData, dwAccelData;

      int iIndex = 0;
      if(count) {
        while(iIndex < count) {
          dwIDAccelData = data[iIndex++];

          WORD wIDCommand = LOWORD(dwIDAccelData);
          CMapWordToCCmdAccelOb::iterator it = m_mapAccelTable.find(wIDCommand);
          bool bExistID = (it != m_mapAccelTable.end());

          if (bExistID) {
            pCmdAccel = it->second;
            pCmdAccel->DeleteUserAccels();
          }
          for (int j = 0; j < HIWORD(dwIDAccelData) && iIndex < count; j++) {
            dwAccelData = data[iIndex++];
            if (bExistID) {
              pAccel = new CAccelsOb;
              ASSERT(pAccel != NULL);
              pAccel->SetData(dwAccelData);
              pCmdAccel->Add(pAccel);
            }
          }
        }
      }
      UpdateWndTable();
      return true;
    }
    return false;
  }
  return true;
}
//////////////////////////////////////////////////////////////////////
// In/Out to the registry
//////////////////////////////////////////////////////////////////////
//
//
bool CAcceleratorManager::Load(HKEY hRegKey, LPCTSTR szRegKey)
{
  ASSERT(szRegKey != NULL);

  m_hRegKey = hRegKey;
  m_szRegKey = szRegKey;

  DWORD data[2048/sizeof(DWORD)];

  DWORD len = sizeof(data);
  if(regQueryBinaryValue("keyboard", (char *)data, len)) {
    int count = len/sizeof(DWORD);

    CCmdAccelOb* pCmdAccel;
    CAccelsOb* pAccel;
    DWORD dwIDAccelData, dwAccelData;
    BOOL bExistID;
    int iIndex = 0;
    if(count) {
      WORD wKey;
      POSITION pos = m_mapAccelTable.GetStartPosition();

      while(pos != NULL) {
        m_mapAccelTable.GetNextAssoc(pos, wKey, pCmdAccel);
        pCmdAccel->DeleteUserAccels();
      }

      while(iIndex < count) {
        dwIDAccelData = data[iIndex++];

        WORD wIDCommand = LOWORD(dwIDAccelData);
        bExistID = m_mapAccelTable.Lookup(wIDCommand, pCmdAccel);

        if (bExistID) {
          pCmdAccel->DeleteUserAccels();
        }
        for (int j = 0; j < HIWORD(dwIDAccelData) && iIndex < count; j++) {
          dwAccelData = data[iIndex++];
          if (bExistID) {
            pAccel = new CAccelsOb;
            ASSERT(pAccel != NULL);
            pAccel->SetData(dwAccelData);
            pCmdAccel->Add(pAccel);
          }
        }
      }
    }
    UpdateWndTable();
    return true;
  }
  return false;
}
bool CAcceleratorManager::AddAccel(BYTE cVirt, WORD wIDCommand, WORD wKey, LPCTSTR szCommand, bool bLocked)
{
  ASSERT(szCommand != NULL);

  //    WORD wIDCmd;
  CMapStringToWord::iterator it;

  it = m_mapAccelString.find((char *)szCommand);

  if (it != m_mapAccelString.end()) {
    if (it->second != wIDCommand)
      return false;
  }

  CCmdAccelOb* pCmdAccel = NULL;
  CMapWordToCCmdAccelOb::iterator it2;

  it2 = m_mapAccelTable.find(wIDCommand);

  if(it2 != m_mapAccelTable.end()) {
    pCmdAccel = it2->second;
    if(strcmp(pCmdAccel->m_szCommand, szCommand)) {
      return false;
    }
    CAccelsOb* pAccel;

    std::list<CAccelsOb *>::iterator it3 = pCmdAccel->m_Accels.begin();
    while (it3 != pCmdAccel->m_Accels.end()) {
      pAccel = *it3;
      if (pAccel->m_cVirt == cVirt &&
          pAccel->m_wKey == wKey)
        return FALSE;
      it3++;
    }
    // Adding the accelerator
    pCmdAccel->Add(cVirt, wKey, bLocked);

  } else {
    pCmdAccel = new CCmdAccelOb(cVirt, wIDCommand, wKey, szCommand, bLocked);
    ASSERT(pCmdAccel != NULL);
    m_mapAccelTable.insert(CMapWordToCCmdAccelOb::value_type(wIDCommand, pCmdAccel));
  }
  // 2nd table
  m_mapAccelString.insert(CMapStringToWord::value_type((char *)szCommand, wIDCommand));
  return true;
}
bool CAcceleratorManager::Default()
{
  CCmdAccelOb* pCmdAccel;
  CCmdAccelOb* pSavedCmdAccel;

  CAccelsOb* pAccel;
  CAccelsOb* pSavedAccel;

  WORD wKey;

  CMapWordToCCmdAccelOb::iterator it = m_mapAccelTable.begin();

  while(it != m_mapAccelTable.end()) {
    wKey = it->first;
    pCmdAccel = it->second;
    pCmdAccel->DeleteUserAccels();
    it++;
  }

// @ Add the saved Accelerators
  it = m_mapAccelTableSaved.begin();
        while (it != m_mapAccelTableSaved.end()) {
    wKey = it->first;
    pSavedCmdAccel = it->second;
    it++;

    pCmdAccel = m_mapAccelTable.find(wKey)->second;

                // @ Removed by kukac  pCmdAccel->DeleteUserAccels();
    std::list<CAccelsOb*>::iterator it = pSavedCmdAccel->m_Accels.begin();
                while (it != pSavedCmdAccel->m_Accels.end()) {
                        pSavedAccel = *it;
      it++;
                        pAccel = new CAccelsOb(pSavedAccel);
                        ASSERT(pAccel != NULL);
                        pCmdAccel->m_Accels.push_back(pAccel);
                }
        }

  return true;
}