1 /* 2 Copyright (C) 2010-2014 Kristian Duske 3 4 This file is part of TrenchBroom. 5 6 TrenchBroom is free software: you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 TrenchBroom is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with TrenchBroom. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 #ifndef TrenchBroom_KeyboardShortcut 21 #define TrenchBroom_KeyboardShortcut 22 23 #include <wx/accel.h> 24 #include <wx/defs.h> 25 #include <wx/string.h> 26 27 #include <set> 28 29 class wxKeyEvent; 30 31 namespace TrenchBroom { 32 namespace View { 33 class KeyboardShortcut { 34 private: 35 class MacModifierOrder { 36 public: 37 bool operator()(const int lhs, const int rhs) const; 38 }; 39 40 class WinModifierOrder { 41 public: 42 bool operator()(const int lhs, const int rhs) const; 43 }; 44 45 #if defined __APPLE__ 46 typedef std::set<int, MacModifierOrder> ModifierSet; 47 #else 48 typedef std::set<int, WinModifierOrder> ModifierSet; 49 #endif 50 public: 51 static const KeyboardShortcut Empty; 52 53 static void sortModifierKeys(int& key1, int& key2, int& key3); 54 static bool isShortcutValid(const int key, const int modifier1 = WXK_NONE, const int modifier2 = WXK_NONE, const int modifier3 = WXK_NONE); 55 56 static wxString shortcutDisplayString(int key, int modifier1, int modifier2, int modifier3); 57 58 static wxString keyMenuString(const int key); 59 static wxString keyDisplayString(const int key); 60 61 static wxString modifierMenuString(const int key); 62 static wxString modifierDisplayString(const int key); 63 64 static bool parseShortcut(const wxString& string, int& key, int& modifier1, int& modifier2, int& modifier3); 65 static int parseKeyDisplayString(const wxString& string); 66 private: 67 int m_key; 68 int m_modifier1; 69 int m_modifier2; 70 int m_modifier3; 71 public: 72 KeyboardShortcut(int key = WXK_NONE, int modifier1 = WXK_NONE, int modifier2 = WXK_NONE, int modifier3 = WXK_NONE); 73 KeyboardShortcut(const wxString& string); 74 75 bool operator<(const KeyboardShortcut& other) const; 76 bool operator==(const KeyboardShortcut& other) const; 77 int compare(const KeyboardShortcut& other) const; 78 79 int key() const; 80 bool hasKey() const; 81 82 int modifier1() const; 83 int modifier2() const; 84 int modifier3() const; 85 bool hasModifier() const; 86 87 bool hasModifier(size_t index) const; 88 int modifier(size_t index) const; 89 90 wxAcceleratorEntry acceleratorEntry(int id) const; 91 int acceleratorFlags() const; 92 93 bool matches(const wxKeyEvent& event) const; 94 bool matches(const int key, const int modifier1 = WXK_NONE, const int modifier2 = WXK_NONE, const int modifier3 = WXK_NONE) const; 95 bool matchesKey(const wxKeyEvent& event) const; 96 bool alwaysShowModifier() const; 97 98 wxString shortcutMenuString() const; 99 wxString shortcutMenuItemString(const wxString& name) const; 100 wxString shortcutDisplayString() const; 101 102 wxString keyMenuString() const; 103 wxString keyDisplayString() const; 104 105 wxString modifierMenuString() const; 106 107 wxString asJsonString() const; 108 wxString asString() const; 109 }; 110 } 111 } 112 113 #endif /* defined(TrenchBroom_KeyboardShortcut) */ 114