|
|
1.1 root 1: /*
2: Copyright (c) 2008 TrueCrypt Foundation. All rights reserved.
3:
4: Governed by the TrueCrypt License 2.4 the full text of which is contained
5: in the file License.txt included in TrueCrypt binary and source code
6: distribution packages.
7: */
8:
9: #include "System.h"
10: #include "Application.h"
11: #include "LanguageStrings.h"
12: #include "GraphicUserInterface.h"
13: #include "Hotkey.h"
14: #include "Xml.h"
15:
16: namespace TrueCrypt
17: {
18: HotkeyList Hotkey::GetAvailableHotkeys ()
19: {
20: HotkeyList hotkeys;
21: #ifdef TC_WINDOWS
22:
23: #define TC_HOTKEY(ID,LANG) hotkeys.push_back (shared_ptr <Hotkey> (new Hotkey (Id::##ID, L###ID, LangString[LANG])))
24:
25: TC_HOTKEY (DismountAll, "HK_DISMOUNT_ALL");
26: TC_HOTKEY (ForceDismountAllWipeCache, "HK_FORCE_DISMOUNT_ALL_AND_WIPE");
27: TC_HOTKEY (ForceDismountAllWipeCacheExit, "HK_FORCE_DISMOUNT_ALL_AND_WIPE_AND_EXIT");
28: TC_HOTKEY (MountAllDevices, "HK_AUTOMOUNT_DEVICES");
29: TC_HOTKEY (MountAllFavorites, "HK_MOUNT_FAVORITE_VOLUMES");
30: TC_HOTKEY (ShowHideApplication, "HK_SHOW_HIDE_MAIN_WINDOW");
31: TC_HOTKEY (WipeCache, "HK_WIPE_CACHE");
32:
33: #endif
34: return hotkeys;
35: }
36:
37: wxString Hotkey::GetShortcutString () const
38: {
39: wxString keyStr = Hotkey::GetVirtualKeyCodeString (VirtualKeyCode);
40: if (keyStr.empty())
41: return L"";
42:
43: wxString str;
44:
45: if (VirtualKeyModifiers & wxMOD_SHIFT)
46: str += LangString["VK_SHIFT"] + L"+";
47:
48: if (VirtualKeyModifiers & wxMOD_CONTROL)
49: str += LangString["VK_CONTROL"] + L"+";
50:
51: if (VirtualKeyModifiers & wxMOD_ALT)
52: str += LangString["VK_ALT"] + L"+";
53:
54: if (VirtualKeyModifiers & wxMOD_WIN )
55: str += LangString["VK_WIN"] + L"+";
56:
57: return str + keyStr;
58: }
59:
60: wxString Hotkey::GetVirtualKeyCodeString (int virtualKeyCode)
61: {
62: #ifdef TC_WINDOWS
63: // ASCII characters
64: if (virtualKeyCode >= 0x30 && virtualKeyCode <= 0x5a)
65: return StringFormatter (L"{0}", char (virtualKeyCode));
66:
67: // OEM-specific
68: if (virtualKeyCode >= 0xE9 && virtualKeyCode <= 0xF5)
69: return StringFormatter (L"OEM-{0}", virtualKeyCode);
70:
71: // F1-F24
72: if (virtualKeyCode >= VK_F1 && virtualKeyCode <= VK_F24)
73: return StringFormatter (L"F{0}", virtualKeyCode - VK_F1 + 1);
74:
75: // Numpad numbers
76: if (virtualKeyCode >= VK_NUMPAD0 && virtualKeyCode <= VK_NUMPAD9)
77: return StringFormatter (L"{0} {1}", LangString["VK_NUMPAD"], virtualKeyCode - VK_NUMPAD0);
78:
79: switch (virtualKeyCode)
80: {
81: case VK_MULTIPLY: return LangString["VK_NUMPAD"] + L" *";
82: case VK_ADD: return LangString["VK_NUMPAD"] + L" +";
83: case VK_SEPARATOR: return LangString["VK_NUMPAD"] + L" Separator";
84: case VK_SUBTRACT: return LangString["VK_NUMPAD"] + L" -";
85: case VK_DECIMAL: return LangString["VK_NUMPAD"] + L" .";
86: case VK_DIVIDE: return LangString["VK_NUMPAD"] + L" /";
87: case VK_OEM_1: return L"OEM 1 (';')";
88: case VK_OEM_PLUS: return L"+";
89: case VK_OEM_COMMA: return L",";
90: case VK_OEM_MINUS: return L"-";
91: case VK_OEM_PERIOD: return L".";
92: case VK_OEM_2: return L"OEM 2 ('/')";
93: case VK_OEM_3: return L"OEM 3 (`)";
94: case VK_OEM_4: return L"OEM 4 ('[')";
95: case VK_OEM_5: return L"OEM 5 ('\\')";
96: case VK_OEM_6: return L"OEM 6 (']')";
97: case VK_OEM_7: return L"OEM 7 (')";
98: case VK_OEM_8: return L"OEM 8";
99: case VK_OEM_AX: return L"OEM AX";
100: case VK_OEM_102: return L"OEM 102";
101: case VK_ICO_HELP: return L"ICO_HELP";
102: case VK_ICO_00: return L"ICO_00";
103: case VK_ICO_CLEAR: return L"ICO_CLEAR";
104: case VK_ATTN: return L"Attn";
105: case VK_CRSEL: return L"CrSel";
106: case VK_EXSEL: return L"ExSel";
107: case VK_EREOF: return L"Erase EOF";
108: case VK_PA1: return L"PA1";
109: case VK_OEM_CLEAR: return L"OEM Clear";
110:
111: case 0:
112: case 1:
113: case 0xFF:
114: break;
115:
116: default:
117: {
118: string langStrId = StringConverter::ToSingle (wstring (wxString::Format (L"VKEY_%02X", virtualKeyCode)));
119: if (LangString.Exists (langStrId))
120: return LangString[langStrId];
121: }
122: }
123: #endif // TC_WINDOWS
124: return L"";
125: }
126:
127: HotkeyList Hotkey::LoadList ()
128: {
129: HotkeyList hotkeys = GetAvailableHotkeys();
130:
131: FilePath path = Application::GetConfigFilePath (GetFileName());
132: if (path.IsFile())
133: {
134: foreach (XmlNode node, XmlParser (path).GetNodes (L"hotkey"))
135: {
136: wstring keyName (node.Attributes[L"name"]);
137:
138: foreach (shared_ptr <Hotkey> hotkey, hotkeys)
139: {
140: if (hotkey->Name == keyName)
141: {
142: hotkey->VirtualKeyCode = StringConverter::ToUInt32 (wstring (node.Attributes[L"vkeycode"]));
143: hotkey->VirtualKeyModifiers = 0;
144:
145: if (node.Attributes[L"modshift"] == L"1")
146: hotkey->VirtualKeyModifiers |= wxMOD_SHIFT;
147:
148: if (node.Attributes[L"modcontrol"] == L"1")
149: hotkey->VirtualKeyModifiers |= wxMOD_CONTROL;
150:
151: if (node.Attributes[L"modalt"] == L"1")
152: hotkey->VirtualKeyModifiers |= wxMOD_ALT;
153:
154: if (node.Attributes[L"modwin"] == L"1")
155: hotkey->VirtualKeyModifiers |= wxMOD_WIN;
156:
157: break;
158: }
159: }
160: }
161: }
162:
163: return hotkeys;
164: }
165:
166: void Hotkey::RegisterList (wxWindow *handler, const HotkeyList &hotkeys)
167: {
168: #ifdef TC_WINDOWS
169: bool res = true;
170: foreach (shared_ptr <Hotkey> hotkey, hotkeys)
171: {
172: if (hotkey->VirtualKeyCode != 0)
173: {
174: if (!handler->RegisterHotKey (hotkey->Id, hotkey->VirtualKeyModifiers, hotkey->VirtualKeyCode))
175: res = false;
176: }
177: }
178:
179: if (!res)
180: Gui->ShowWarning ("HOTKEY_REGISTRATION_ERROR");
181: #endif
182: }
183:
184: void Hotkey::SaveList (const HotkeyList &hotkeys)
185: {
186: FilePath hotkeysCfgPath = Application::GetConfigFilePath (GetFileName(), true);
187:
188: bool noHotkey = true;
189: XmlNode hotkeysXml (L"hotkeys");
190: foreach_ref (const Hotkey &hotkey, hotkeys)
191: {
192: if (hotkey.VirtualKeyCode == 0)
193: continue;
194:
195: noHotkey = false;
196: XmlNode node (L"hotkey");
197: node.Attributes[L"name"] = wstring (hotkey.Name);
198:
199: node.Attributes[L"vkeycode"] = StringConverter::FromNumber (hotkey.VirtualKeyCode);
200:
201: if (hotkey.VirtualKeyModifiers & wxMOD_SHIFT)
202: node.Attributes[L"modshift"] = L"1";
203:
204: if (hotkey.VirtualKeyModifiers & wxMOD_CONTROL)
205: node.Attributes[L"modcontrol"] = L"1";
206:
207: if (hotkey.VirtualKeyModifiers & wxMOD_ALT)
208: node.Attributes[L"modalt"] = L"1";
209:
210: if (hotkey.VirtualKeyModifiers & wxMOD_WIN )
211: node.Attributes[L"modwin"] = L"1";
212:
213: hotkeysXml.InnerNodes.push_back (node);
214: }
215:
216: if (noHotkey)
217: {
218: if (hotkeysCfgPath.IsFile())
219: hotkeysCfgPath.Delete();
220: }
221: else
222: {
223: XmlWriter hotkeysWriter (hotkeysCfgPath);
224: hotkeysWriter.WriteNode (hotkeysXml);
225: hotkeysWriter.Close();
226: }
227: }
228:
229: void Hotkey::UnregisterList (wxWindow *handler, const HotkeyList &hotkeys)
230: {
231: #ifdef TC_WINDOWS
232: foreach (shared_ptr <Hotkey> hotkey, hotkeys)
233: {
234: if (hotkey->VirtualKeyCode != 0)
235: handler->UnregisterHotKey (hotkey->Id);
236: }
237: #endif
238: }
239: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.