Commits

Stefan Cosma committed 49b7322

Check for duplicate hotkeys

  • Participants
  • Parent commits 70fbdef

Comments (0)

Files changed (6)

File Plugin/ConfigDialog.cpp

 
 	HotKey hk = hkDialog.GetHotKey();
 
-	//TODO: Check for duplicates
+	if (IsDefined(hk))
+	{
+		TCHAR text[256] = {0};
+
+		::LoadString(GetApp()->GetInstanceHandle(), IDS_DUPLICATE_ERROR_TEXT,
+				text, sizeof(text)/sizeof(text[0]));
+
+		TCHAR caption[256] = {0};
+
+		::LoadString(GetApp()->GetInstanceHandle(), IDS_DUPLICATE_ERROR_CAPTION,
+				caption, sizeof(caption)/sizeof(caption[0]));
+
+		MessageBox(text, caption, MB_OK | MB_ICONERROR | MB_APPLMODAL);
+
+		return;
+	}
+
 	m_hotkeys.push_back(hk);
 	m_listView.InsertHotkey(hk);
 }
 
 	HotKey hk = hkDialog.GetHotKey();
 
-	//TODO: Check for duplicates
+	if (IsDefined(hk))
+	{
+		TCHAR text[256] = {0};
+
+		::LoadString(GetApp()->GetInstanceHandle(), IDS_DUPLICATE_ERROR_TEXT,
+				text, sizeof(text)/sizeof(text[0]));
+
+		TCHAR caption[256] = {0};
+
+		::LoadString(GetApp()->GetInstanceHandle(), IDS_DUPLICATE_ERROR_CAPTION,
+				caption, sizeof(caption)/sizeof(caption[0]));
+
+		MessageBox(text, caption, MB_OK | MB_ICONERROR | MB_APPLMODAL);
+
+		return;
+	}
+
 	m_hotkeys[index].keycomb = hk.keycomb;
 	m_listView.UpdateHotkey(itemIndex, hk.keycomb);
 }

File Plugin/HotKeyManager.cpp

 
 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
+bool HotKeyManager::IsDefined(HotKey otherhk)
+{
+	for (HotKeysIterator it = m_hotkeys.begin(); it != m_hotkeys.end(); ++it)
+	{
+		HotKey hk = (*it);
+
+		if (hk.keycomb == otherhk.keycomb)
+		{
+			return true;
+		}
+	}
+
+	return false;
+}
+
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
 void HotKeyManager::SaveHotkeys()
 {
 	using namespace Json;

File Plugin/HotKeyManager.h

 	void SaveHotkeys();
 	void LoadHotkeys();
 
+	bool IsDefined(HotKey hk);
+
 private:
 	const string_t		GetConfigFileDir() const;
 	const string_t		GetConfigFilePath() const;

File Plugin/hotkeys.h

 	bool			control;
 	bool			shift;
 	bool			meta;		// win logo
+
+	bool operator ==(const KeyCombination& other) const { return key == other.key && alt == other.alt && control == other.control && shift == other.shift && meta == other.meta; }
+	bool operator !=(const KeyCombination& other) const { return key != other.key && alt != other.alt && control != other.control && shift != other.shift && meta != other.meta; }
 };
 
 struct HotKey

File Plugin/resource.h

 #define IDS_HOTKEY_DIALOG_CAPTION       148
 #define IDS_HOTKEY_DIALOG_TEXT          149
 #define IDS_VK_UNKNOWN                  150
+#define IDS_DUPLICATE_ERROR_TEXT        151
+#define IDS_DUPLICATE_ERROR_CAPTION     152
 #define IDC_APPLY                       1001
 #define IDC_HOTKEYS_LIST                1002
 #define IDC_ACTIONS_COMBO               1003

File Plugin/resource.rc

     IDS_HOTKEY_DIALOG_CAPTION "Hotkey for %s"
     IDS_HOTKEY_DIALOG_TEXT  "Press the new key combination for %s"
     IDS_VK_UNKNOWN          "Key %#x"
+    IDS_DUPLICATE_ERROR_TEXT "Hotkey already defined"
+    IDS_DUPLICATE_ERROR_CAPTION "Duplicate Hotkey"
 END
 
 #endif    // English (United States) resources