home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 July / CMCD0704.ISO / Software / Freeware / Utilitare / VisualBoyAdvance-1.7.2 / src / win32 / AccelEditor.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-05-13  |  7.9 KB  |  290 lines

  1. // VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator.
  2. // Copyright (C) 1999-2003 Forgotten
  3. // Copyright (C) 2004 Forgotten and the VBA development team
  4.  
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 2, or(at your option)
  8. // any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software Foundation,
  17. // Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18.  
  19. // AccelEditor.cpp : implementation file
  20. //
  21.  
  22. #include "stdafx.h"
  23. #include "vba.h"
  24. #include "AccelEditor.h"
  25. #include "CmdAccelOb.h"
  26.  
  27. #ifdef _DEBUG
  28. #define new DEBUG_NEW
  29. #undef THIS_FILE
  30. static char THIS_FILE[] = __FILE__;
  31. #endif
  32.  
  33. /////////////////////////////////////////////////////////////////////////////
  34. // AccelEditor dialog
  35.  
  36.  
  37. AccelEditor::AccelEditor(CWnd* pParent /*=NULL*/)
  38.   : ResizeDlg(AccelEditor::IDD, pParent)
  39. {
  40.   //{{AFX_DATA_INIT(AccelEditor)
  41.   // NOTE: the ClassWizard will add member initialization here
  42.   //}}AFX_DATA_INIT
  43.   mgr = theApp.winAccelMgr;
  44. }
  45.  
  46.  
  47. void AccelEditor::DoDataExchange(CDataExchange* pDX)
  48. {
  49.   CDialog::DoDataExchange(pDX);
  50.   //{{AFX_DATA_MAP(AccelEditor)
  51.   DDX_Control(pDX, IDC_CURRENTS, m_currents);
  52.   DDX_Control(pDX, IDC_ALREADY_AFFECTED, m_alreadyAffected);
  53.   DDX_Control(pDX, IDC_COMMANDS, m_commands);
  54.   DDX_Control(pDX, IDC_EDIT_KEY, m_key);
  55.   //}}AFX_DATA_MAP
  56. }
  57.  
  58.  
  59. BEGIN_MESSAGE_MAP(AccelEditor, CDialog)
  60.   //{{AFX_MSG_MAP(AccelEditor)
  61.   ON_BN_CLICKED(ID_OK, OnOk)
  62.   ON_LBN_SELCHANGE(IDC_COMMANDS, OnSelchangeCommands)
  63.   ON_BN_CLICKED(IDC_RESET, OnReset)
  64.   ON_BN_CLICKED(IDC_ASSIGN, OnAssign)
  65.   ON_BN_CLICKED(ID_CANCEL, OnCancel)
  66.   ON_BN_CLICKED(IDC_REMOVE, OnRemove)
  67.   //}}AFX_MSG_MAP
  68.   END_MESSAGE_MAP()
  69.  
  70.   /////////////////////////////////////////////////////////////////////////////
  71. // AccelEditor message handlers
  72.  
  73. BOOL AccelEditor::OnInitDialog() 
  74. {
  75.   CDialog::OnInitDialog();
  76.   
  77.   DIALOG_SIZER_START( sz )
  78.     DIALOG_SIZER_ENTRY( IDC_STATIC1, DS_MoveX)
  79.     DIALOG_SIZER_ENTRY( IDC_STATIC2, DS_MoveY)
  80.     DIALOG_SIZER_ENTRY( IDC_STATIC3, DS_MoveX | DS_MoveY)
  81.     DIALOG_SIZER_ENTRY( IDC_ALREADY_AFFECTED, DS_MoveY)
  82.     DIALOG_SIZER_ENTRY( ID_OK, DS_MoveX)
  83.     DIALOG_SIZER_ENTRY( ID_CANCEL, DS_MoveX)
  84.     DIALOG_SIZER_ENTRY( IDC_ASSIGN, DS_MoveX)
  85.     DIALOG_SIZER_ENTRY( IDC_REMOVE, DS_MoveX)
  86.     DIALOG_SIZER_ENTRY( IDC_RESET, DS_MoveX)
  87.     DIALOG_SIZER_ENTRY( IDC_CLOSE, DS_MoveY)
  88.     DIALOG_SIZER_ENTRY( IDC_COMMANDS, DS_SizeX | DS_SizeY)
  89.     DIALOG_SIZER_ENTRY( IDC_CURRENTS, DS_MoveX | DS_SizeY)
  90.     DIALOG_SIZER_ENTRY( IDC_EDIT_KEY, DS_MoveX | DS_MoveY)
  91.     DIALOG_SIZER_END()
  92.  
  93.     SetData(sz,
  94.             TRUE,
  95.             HKEY_CURRENT_USER,
  96.             "Software\\Emulators\\VisualBoyAdvance\\Viewer\\AccelEditor",
  97.             NULL);
  98.  
  99.   InitCommands();
  100.   
  101.   return TRUE;  // return TRUE unless you set the focus to a control
  102.                 // EXCEPTION: OCX Property Pages should return FALSE
  103. }
  104.  
  105. void AccelEditor::InitCommands()
  106. {
  107.   m_commands.ResetContent();
  108.   m_alreadyAffected.SetWindowText("");
  109.  
  110.   POSITION pos = mgr.m_mapAccelString.GetStartPosition();
  111.   
  112.   while(pos != NULL) {
  113.     CString command;
  114.     WORD wID;
  115.     mgr.m_mapAccelString.GetNextAssoc(pos, command, wID);
  116.  
  117.     int index = m_commands.AddString(command);
  118.     m_commands.SetItemData(index, wID);
  119.   }
  120.  
  121.   // Update the currents accels associated with the selected command
  122.   if (m_commands.SetCurSel(0) != LB_ERR)
  123.     OnSelchangeCommands();
  124. }
  125.  
  126. void AccelEditor::OnCancel() 
  127. {
  128.   EndDialog(FALSE);
  129. }
  130.  
  131. void AccelEditor::OnOk() 
  132. {
  133.   EndDialog(TRUE);
  134. }
  135.  
  136. void AccelEditor::OnSelchangeCommands() 
  137. {
  138.   // Check if some commands exist.
  139.   int index = m_commands.GetCurSel();
  140.   if (index == LB_ERR)
  141.     return;
  142.  
  143.   WORD wIDCommand = LOWORD(m_commands.GetItemData(index));
  144.   m_currents.ResetContent();
  145.  
  146.   CCmdAccelOb* pCmdAccel;
  147.   
  148.   if (mgr.m_mapAccelTable.Lookup(wIDCommand, pCmdAccel)) {
  149.     CAccelsOb* pAccel;
  150.     CString szBuffer;
  151.     POSITION pos = pCmdAccel->m_Accels.GetHeadPosition();
  152.  
  153.     // Add the keys to the 'currents keys' listbox.
  154.     while (pos != NULL) {
  155.       pAccel = pCmdAccel->m_Accels.GetNext(pos);
  156.       pAccel->GetString(szBuffer);
  157.       index = m_currents.AddString(szBuffer);
  158.       // and a pointer to the accel object.
  159.       m_currents.SetItemData(index, (DWORD)pAccel);
  160.     }
  161.   }
  162.   // Init the key editor
  163.   //  m_pKey->ResetKey();
  164.  
  165. }
  166.  
  167. void AccelEditor::OnReset() 
  168. {
  169.   mgr.Default();
  170.   InitCommands(); // update the listboxes.
  171. }
  172.  
  173. void AccelEditor::OnAssign() 
  174. {
  175.   // Control if it's not already affected
  176.   CCmdAccelOb* pCmdAccel;
  177.   CAccelsOb* pAccel;
  178.   WORD wIDCommand;
  179.   POSITION pos;
  180.   
  181.   WORD wKey;
  182.   bool bCtrl, bAlt, bShift;
  183.  
  184.   if (!m_key.GetAccelKey(wKey, bCtrl, bAlt, bShift))
  185.     return; // no valid key, abort
  186.  
  187.   int count = m_commands.GetCount();
  188.   for (int index = 0; index < count; index++) {
  189.  
  190.     wIDCommand = LOWORD(m_commands.GetItemData(index));
  191.     mgr.m_mapAccelTable.Lookup(wIDCommand, pCmdAccel);
  192.  
  193.     pos = pCmdAccel->m_Accels.GetHeadPosition();
  194.     while (pos != NULL) {
  195.       pAccel = pCmdAccel->m_Accels.GetNext(pos);
  196.       if (pAccel->IsEqual(wKey, bCtrl, bAlt, bShift)) {
  197.         // the key is already affected (in the same or other command)
  198.         m_alreadyAffected.SetWindowText(pCmdAccel->m_szCommand);
  199.         m_key.SetSel(0, -1);
  200.         return; // abort
  201.       }
  202.     }
  203.   }
  204.  
  205.   // OK, we can add the accel key in the currently selected group
  206.   index = m_commands.GetCurSel();
  207.   if (index == LB_ERR)
  208.     return;
  209.  
  210.   // Get the object who manage the accels list, associated to the command.
  211.   wIDCommand = LOWORD(m_commands.GetItemData(index));
  212.  
  213.   if (mgr.m_mapAccelTable.Lookup(wIDCommand, pCmdAccel) != TRUE)
  214.     return;
  215.  
  216.   BYTE cVirt = 0;
  217.   if (bCtrl)
  218.     cVirt |= FCONTROL;
  219.   if (bAlt)
  220.     cVirt |= FALT;
  221.   if (bShift)
  222.     cVirt |= FSHIFT;
  223.  
  224.   cVirt |= FVIRTKEY;
  225.  
  226.   // Create the new key...
  227.   pAccel = new CAccelsOb(cVirt, wKey, false);
  228.   ASSERT(pAccel != NULL);
  229.   // ...and add in the list.
  230.   pCmdAccel->m_Accels.AddTail(pAccel);
  231.  
  232.   // Update the listbox.
  233.   CString szBuffer;
  234.   pAccel->GetString(szBuffer);
  235.  
  236.   index = m_currents.AddString(szBuffer);
  237.   m_currents.SetItemData(index, (DWORD)pAccel);
  238.  
  239.   // Reset the key editor.
  240.   m_key.ResetKey();
  241. }
  242.  
  243. void AccelEditor::OnRemove() 
  244. {
  245.   // Some controls
  246.   int indexCurrent = m_currents.GetCurSel();
  247.   if (indexCurrent == LB_ERR)
  248.     return;
  249.   
  250.   // 2nd part.
  251.   int indexCmd = m_commands.GetCurSel();
  252.   if (indexCmd == LB_ERR)
  253.     return;
  254.  
  255.   // Ref to the ID command
  256.   WORD wIDCommand = LOWORD(m_commands.GetItemData(indexCmd));
  257.  
  258.   // Run through the accels,and control if it can be deleted.
  259.   CCmdAccelOb* pCmdAccel;
  260.   if (mgr.m_mapAccelTable.Lookup(wIDCommand, pCmdAccel) == TRUE) {
  261.     CAccelsOb* pAccel;
  262.     CAccelsOb* pAccelCurrent = (CAccelsOb*)(m_currents.GetItemData(indexCurrent));
  263.     CString szBuffer;
  264.     POSITION pos = pCmdAccel->m_Accels.GetHeadPosition();
  265.     POSITION PrevPos;
  266.     while (pos != NULL) {
  267.       PrevPos = pos;
  268.       pAccel = pCmdAccel->m_Accels.GetNext(pos);
  269.       if (pAccel == pAccelCurrent) {
  270.         if (!pAccel->m_bLocked) {
  271.           // not locked, so we delete the key
  272.           pCmdAccel->m_Accels.RemoveAt(PrevPos);
  273.           delete pAccel;
  274.           // and update the listboxes/key editor/static text
  275.           m_currents.DeleteString(indexCurrent);
  276.           m_key.ResetKey();
  277.           m_alreadyAffected.SetWindowText("");
  278.           return;
  279.         } else {
  280.           systemMessage(0,"Unable to remove this\naccelerator (Locked)");
  281.           return;
  282.         }
  283.       }
  284.     }
  285.     systemMessage(0,"internal error (CAccelDlgHelper::Remove : pAccel unavailable)");
  286.     return;
  287.   }
  288.   systemMessage(0,"internal error (CAccelDlgHelper::Remove : Lookup failed)");
  289. }
  290.