HotKeys.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. #include "stdafx.h"
  2. #include "HotKeys.h"
  3. #include "Options.h"
  4. #include "Misc.h"
  5. CHotKeys g_HotKeys;
  6. CHotKey::CHotKey(CString name, DWORD defKey, bool bUnregOnShowDitto)
  7. : m_Name(name),
  8. m_bIsRegistered(false),
  9. m_bUnRegisterOnShowDitto(bUnregOnShowDitto)
  10. {
  11. m_Atom = ::GlobalAddAtom(m_Name);
  12. ASSERT(m_Atom);
  13. m_Key = (DWORD)g_Opt.GetProfileLong(m_Name, (long) defKey);
  14. g_HotKeys.Add(this);
  15. }
  16. CHotKey::~CHotKey()
  17. {
  18. Unregister();
  19. }
  20. void CHotKey::SetKey( DWORD key, bool bSave )
  21. {
  22. if(m_Key == key)
  23. {
  24. return;
  25. }
  26. if(m_bIsRegistered)
  27. Unregister();
  28. m_Key = key;
  29. if(bSave)
  30. SaveKey();
  31. }
  32. void CHotKey::LoadKey()
  33. {
  34. SetKey((DWORD) g_Opt.GetProfileLong(m_Name, 0));
  35. }
  36. bool CHotKey::SaveKey()
  37. {
  38. if(m_clipId > 0)
  39. {
  40. return g_Opt.SetProfileLong( m_Name, (long) m_Key ) != FALSE;
  41. }
  42. return false;
  43. }
  44. BOOL CHotKey::ValidateHotKey(DWORD dwHotKey)
  45. {
  46. ATOM id = ::GlobalAddAtom(_T("HK_VALIDATE"));
  47. BOOL bResult = ::RegisterHotKey( g_HotKeys.m_hWnd,
  48. id,
  49. GetModifier(dwHotKey),
  50. LOBYTE(dwHotKey) );
  51. if(bResult)
  52. ::UnregisterHotKey(g_HotKeys.m_hWnd, id);
  53. ::GlobalDeleteAtom(id);
  54. return bResult;
  55. }
  56. void CHotKey::CopyFromCtrl(CHotKeyCtrl& ctrl, HWND hParent, int nWindowsCBID)
  57. {
  58. long lHotKey = ctrl.GetHotKey();
  59. short sKeyKode = LOBYTE(lHotKey);
  60. short sModifers = HIBYTE(lHotKey);
  61. if(lHotKey && ::IsDlgButtonChecked(hParent, nWindowsCBID))
  62. {
  63. sModifers |= HOTKEYF_EXT;
  64. }
  65. SetKey(MAKEWORD(sKeyKode, sModifers));
  66. }
  67. void CHotKey::CopyToCtrl(CHotKeyCtrl& ctrl, HWND hParent, int nWindowsCBID)
  68. {
  69. long lModifiers = HIBYTE(m_Key);
  70. ctrl.SetHotKey(LOBYTE(m_Key), (WORD)lModifiers);
  71. if(lModifiers & HOTKEYF_EXT)
  72. {
  73. ::CheckDlgButton(hParent, nWindowsCBID, BST_CHECKED);
  74. }
  75. }
  76. UINT CHotKey::GetModifier(DWORD dwHotKey)
  77. {
  78. UINT uMod = 0;
  79. if( HIBYTE(dwHotKey) & HOTKEYF_SHIFT ) uMod |= MOD_SHIFT;
  80. if( HIBYTE(dwHotKey) & HOTKEYF_CONTROL ) uMod |= MOD_CONTROL;
  81. if( HIBYTE(dwHotKey) & HOTKEYF_ALT ) uMod |= MOD_ALT;
  82. if( HIBYTE(dwHotKey) & HOTKEYF_EXT ) uMod |= MOD_WIN;
  83. return uMod;
  84. }
  85. bool CHotKey::Register()
  86. {
  87. if(m_Key)
  88. {
  89. if(m_bIsRegistered == false)
  90. {
  91. ASSERT(g_HotKeys.m_hWnd);
  92. m_bIsRegistered = ::RegisterHotKey(g_HotKeys.m_hWnd,
  93. m_Atom,
  94. GetModifier(),
  95. LOBYTE(m_Key) ) == TRUE;
  96. }
  97. }
  98. else
  99. m_bIsRegistered = true;
  100. return m_bIsRegistered;
  101. }
  102. bool CHotKey::Unregister(bool bOnShowingDitto)
  103. {
  104. if(!m_bIsRegistered)
  105. return true;
  106. if(bOnShowingDitto)
  107. {
  108. if(m_bUnRegisterOnShowDitto == false)
  109. return true;
  110. }
  111. if(m_Key)
  112. {
  113. ASSERT(g_HotKeys.m_hWnd);
  114. if(::UnregisterHotKey( g_HotKeys.m_hWnd, m_Atom))
  115. {
  116. m_bIsRegistered = false;
  117. return true;
  118. }
  119. else
  120. {
  121. Log(_T("Unregister FAILED!"));
  122. ASSERT(0);
  123. }
  124. }
  125. else
  126. {
  127. m_bIsRegistered = false;
  128. return true;
  129. }
  130. return false;
  131. }
  132. CHotKeys::CHotKeys() : m_hWnd(NULL)
  133. {
  134. }
  135. CHotKeys::~CHotKeys()
  136. {
  137. CHotKey* pHotKey;
  138. INT_PTR count = GetSize();
  139. for(int i=0; i < count; i++)
  140. {
  141. pHotKey = ElementAt(i);
  142. if(pHotKey)
  143. {
  144. delete pHotKey;
  145. }
  146. }
  147. }
  148. INT_PTR CHotKeys::Find(CHotKey* pHotKey)
  149. {
  150. INT_PTR count = GetSize();
  151. for(int i=0; i < count; i++)
  152. {
  153. if(pHotKey == ElementAt(i))
  154. {
  155. return i;
  156. }
  157. }
  158. return -1;
  159. }
  160. bool CHotKeys::Remove(CHotKey* pHotKey)
  161. {
  162. INT_PTR i = Find(pHotKey);
  163. if(i >= 0)
  164. {
  165. RemoveAt(i);
  166. return true;
  167. }
  168. return false;
  169. }
  170. bool CHotKeys::Remove(int clipId)
  171. {
  172. INT_PTR count = GetSize();
  173. for(int i=0; i < count; i++)
  174. {
  175. if(ElementAt(i) != NULL && ElementAt(i)->m_clipId == clipId)
  176. {
  177. CHotKey *pKey = ElementAt(i);
  178. RemoveAt(i);
  179. delete pKey;
  180. return true;
  181. }
  182. }
  183. return false;
  184. }
  185. BOOL CHotKeys::ValidateClip(int clipId, DWORD key, CString desc)
  186. {
  187. CHotKey *pKey = NULL;
  188. INT_PTR count = GetSize();
  189. for(int i=0; i < count; i++)
  190. {
  191. if(ElementAt(i) != NULL && ElementAt(i)->m_clipId == clipId)
  192. {
  193. pKey = ElementAt(i);
  194. break;
  195. }
  196. }
  197. if(pKey == NULL)
  198. {
  199. pKey = new CHotKey(desc, key, true);
  200. }
  201. BOOL ret = FALSE;
  202. if(pKey != NULL)
  203. {
  204. pKey->m_Key = key;
  205. pKey->m_Name = desc;
  206. pKey->m_clipId = clipId;
  207. ret = CHotKey::ValidateHotKey(key);
  208. }
  209. return ret;
  210. }
  211. void CHotKeys::LoadAllKeys()
  212. {
  213. INT_PTR count = GetSize();
  214. for(int i=0; i < count; i++)
  215. {
  216. ElementAt(i)->LoadKey();
  217. }
  218. }
  219. void CHotKeys::SaveAllKeys()
  220. {
  221. INT_PTR count = GetSize();
  222. for(int i=0; i < count; i++)
  223. {
  224. ElementAt(i)->SaveKey();
  225. }
  226. }
  227. void CHotKeys::RegisterAll(bool bMsgOnError)
  228. {
  229. CString str;
  230. CHotKey* pHotKey;
  231. INT_PTR count = GetSize();
  232. for(int i = 0; i < count; i++)
  233. {
  234. pHotKey = ElementAt(i);
  235. if(!pHotKey->Register())
  236. {
  237. str = "Error Registering ";
  238. str += pHotKey->GetName();
  239. Log(str);
  240. if(bMsgOnError)
  241. AfxMessageBox(str);
  242. }
  243. }
  244. }
  245. void CHotKeys::UnregisterAll(bool bMsgOnError, bool bOnShowDitto)
  246. {
  247. CString str;
  248. CHotKey* pHotKey;
  249. INT_PTR count = GetSize();
  250. for(int i = 0; i < count; i++)
  251. {
  252. pHotKey = ElementAt(i);
  253. if(!pHotKey->Unregister(bOnShowDitto))
  254. {
  255. str = "Error Unregistering ";
  256. str += pHotKey->GetName();
  257. Log(str);
  258. if(bMsgOnError)
  259. AfxMessageBox(str);
  260. }
  261. }
  262. }
  263. void CHotKeys::GetKeys(ARRAY& keys)
  264. {
  265. INT_PTR count = GetSize();
  266. keys.SetSize(count);
  267. for(int i=0; i < count; i++)
  268. {
  269. keys[i] = ElementAt(i)->GetKey();
  270. }
  271. }
  272. // caution! this alters hotkeys based upon corresponding indexes
  273. void CHotKeys::SetKeys(ARRAY& keys, bool bSave)
  274. {
  275. INT_PTR count = GetSize();
  276. ASSERT(count == keys.GetSize());
  277. for(int i=0; i < count; i++)
  278. {
  279. ElementAt(i)->SetKey(keys[(INT)i], bSave);
  280. }
  281. }
  282. bool CHotKeys::FindFirstConflict(ARRAY& keys, INT_PTR* pX, INT_PTR* pY)
  283. {
  284. bool bConflict = false;
  285. INT_PTR i, j;
  286. INT_PTR count = keys.GetSize();
  287. DWORD key;
  288. for(i = 0; i < count && !bConflict; i++)
  289. {
  290. key = keys.ElementAt(i);
  291. // only check valid keys
  292. if(key == 0)
  293. continue;
  294. // scan the array for a duplicate
  295. for(j = i+1; j < count; j++ )
  296. {
  297. if(keys.ElementAt(j) == key)
  298. {
  299. bConflict = true;
  300. break;
  301. }
  302. }
  303. }
  304. if(bConflict)
  305. {
  306. if(pX)
  307. *pX = i-1;
  308. if(pY)
  309. *pY = j;
  310. }
  311. return bConflict;
  312. }
  313. // if true, pX and pY (if valid) are set to the indexes of the conflicting hotkeys.
  314. bool CHotKeys::FindFirstConflict(INT_PTR* pX, INT_PTR* pY)
  315. {
  316. ARRAY keys;
  317. GetKeys(keys);
  318. return FindFirstConflict(keys, pX, pY);
  319. }