The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

419 lines
14KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. KeyPressMappingSet::KeyPressMappingSet (ApplicationCommandManager& cm)
  18. : commandManager (cm)
  19. {
  20. Desktop::getInstance().addFocusChangeListener (this);
  21. }
  22. KeyPressMappingSet::KeyPressMappingSet (const KeyPressMappingSet& other)
  23. : KeyListener(), ChangeBroadcaster(), FocusChangeListener(),
  24. commandManager (other.commandManager)
  25. {
  26. Desktop::getInstance().addFocusChangeListener (this);
  27. }
  28. KeyPressMappingSet::~KeyPressMappingSet()
  29. {
  30. Desktop::getInstance().removeFocusChangeListener (this);
  31. }
  32. //==============================================================================
  33. Array<KeyPress> KeyPressMappingSet::getKeyPressesAssignedToCommand (const CommandID commandID) const
  34. {
  35. for (int i = 0; i < mappings.size(); ++i)
  36. if (mappings.getUnchecked(i)->commandID == commandID)
  37. return mappings.getUnchecked (i)->keypresses;
  38. return Array<KeyPress>();
  39. }
  40. void KeyPressMappingSet::addKeyPress (const CommandID commandID,
  41. const KeyPress& newKeyPress,
  42. int insertIndex)
  43. {
  44. // If you specify an upper-case letter but no shift key, how is the user supposed to press it!?
  45. // Stick to lower-case letters when defining a keypress, to avoid ambiguity.
  46. jassert (! (CharacterFunctions::isUpperCase (newKeyPress.getTextCharacter())
  47. && ! newKeyPress.getModifiers().isShiftDown()));
  48. if (findCommandForKeyPress (newKeyPress) != commandID)
  49. {
  50. if (newKeyPress.isValid())
  51. {
  52. for (int i = mappings.size(); --i >= 0;)
  53. {
  54. if (mappings.getUnchecked(i)->commandID == commandID)
  55. {
  56. mappings.getUnchecked(i)->keypresses.insert (insertIndex, newKeyPress);
  57. sendChangeMessage();
  58. return;
  59. }
  60. }
  61. if (const ApplicationCommandInfo* const ci = commandManager.getCommandForID (commandID))
  62. {
  63. CommandMapping* const cm = new CommandMapping();
  64. cm->commandID = commandID;
  65. cm->keypresses.add (newKeyPress);
  66. cm->wantsKeyUpDownCallbacks = (ci->flags & ApplicationCommandInfo::wantsKeyUpDownCallbacks) != 0;
  67. mappings.add (cm);
  68. sendChangeMessage();
  69. }
  70. }
  71. }
  72. }
  73. void KeyPressMappingSet::resetToDefaultMappings()
  74. {
  75. mappings.clear();
  76. for (int i = 0; i < commandManager.getNumCommands(); ++i)
  77. {
  78. const ApplicationCommandInfo* const ci = commandManager.getCommandForIndex (i);
  79. for (int j = 0; j < ci->defaultKeypresses.size(); ++j)
  80. {
  81. addKeyPress (ci->commandID,
  82. ci->defaultKeypresses.getReference (j));
  83. }
  84. }
  85. sendChangeMessage();
  86. }
  87. void KeyPressMappingSet::resetToDefaultMapping (const CommandID commandID)
  88. {
  89. clearAllKeyPresses (commandID);
  90. const ApplicationCommandInfo* const ci = commandManager.getCommandForID (commandID);
  91. for (int j = 0; j < ci->defaultKeypresses.size(); ++j)
  92. {
  93. addKeyPress (ci->commandID,
  94. ci->defaultKeypresses.getReference (j));
  95. }
  96. }
  97. void KeyPressMappingSet::clearAllKeyPresses()
  98. {
  99. if (mappings.size() > 0)
  100. {
  101. sendChangeMessage();
  102. mappings.clear();
  103. }
  104. }
  105. void KeyPressMappingSet::clearAllKeyPresses (const CommandID commandID)
  106. {
  107. for (int i = mappings.size(); --i >= 0;)
  108. {
  109. if (mappings.getUnchecked(i)->commandID == commandID)
  110. {
  111. mappings.remove (i);
  112. sendChangeMessage();
  113. }
  114. }
  115. }
  116. void KeyPressMappingSet::removeKeyPress (const KeyPress& keypress)
  117. {
  118. if (keypress.isValid())
  119. {
  120. for (int i = mappings.size(); --i >= 0;)
  121. {
  122. CommandMapping& cm = *mappings.getUnchecked(i);
  123. for (int j = cm.keypresses.size(); --j >= 0;)
  124. {
  125. if (keypress == cm.keypresses [j])
  126. {
  127. cm.keypresses.remove (j);
  128. sendChangeMessage();
  129. }
  130. }
  131. }
  132. }
  133. }
  134. void KeyPressMappingSet::removeKeyPress (const CommandID commandID, const int keyPressIndex)
  135. {
  136. for (int i = mappings.size(); --i >= 0;)
  137. {
  138. if (mappings.getUnchecked(i)->commandID == commandID)
  139. {
  140. mappings.getUnchecked(i)->keypresses.remove (keyPressIndex);
  141. sendChangeMessage();
  142. break;
  143. }
  144. }
  145. }
  146. //==============================================================================
  147. CommandID KeyPressMappingSet::findCommandForKeyPress (const KeyPress& keyPress) const noexcept
  148. {
  149. for (int i = 0; i < mappings.size(); ++i)
  150. if (mappings.getUnchecked(i)->keypresses.contains (keyPress))
  151. return mappings.getUnchecked(i)->commandID;
  152. return 0;
  153. }
  154. bool KeyPressMappingSet::containsMapping (const CommandID commandID, const KeyPress& keyPress) const noexcept
  155. {
  156. for (int i = mappings.size(); --i >= 0;)
  157. if (mappings.getUnchecked(i)->commandID == commandID)
  158. return mappings.getUnchecked(i)->keypresses.contains (keyPress);
  159. return false;
  160. }
  161. void KeyPressMappingSet::invokeCommand (const CommandID commandID,
  162. const KeyPress& key,
  163. const bool isKeyDown,
  164. const int millisecsSinceKeyPressed,
  165. Component* const originatingComponent) const
  166. {
  167. ApplicationCommandTarget::InvocationInfo info (commandID);
  168. info.invocationMethod = ApplicationCommandTarget::InvocationInfo::fromKeyPress;
  169. info.isKeyDown = isKeyDown;
  170. info.keyPress = key;
  171. info.millisecsSinceKeyPressed = millisecsSinceKeyPressed;
  172. info.originatingComponent = originatingComponent;
  173. commandManager.invoke (info, false);
  174. }
  175. //==============================================================================
  176. bool KeyPressMappingSet::restoreFromXml (const XmlElement& xmlVersion)
  177. {
  178. if (xmlVersion.hasTagName ("KEYMAPPINGS"))
  179. {
  180. if (xmlVersion.getBoolAttribute ("basedOnDefaults", true))
  181. {
  182. // if the XML was created as a set of differences from the default mappings,
  183. // (i.e. by calling createXml (true)), then we need to first restore the defaults.
  184. resetToDefaultMappings();
  185. }
  186. else
  187. {
  188. // if the XML was created calling createXml (false), then we need to clear all
  189. // the keys and treat the xml as describing the entire set of mappings.
  190. clearAllKeyPresses();
  191. }
  192. forEachXmlChildElement (xmlVersion, map)
  193. {
  194. const CommandID commandId = map->getStringAttribute ("commandId").getHexValue32();
  195. if (commandId != 0)
  196. {
  197. const KeyPress key (KeyPress::createFromDescription (map->getStringAttribute ("key")));
  198. if (map->hasTagName ("MAPPING"))
  199. {
  200. addKeyPress (commandId, key);
  201. }
  202. else if (map->hasTagName ("UNMAPPING"))
  203. {
  204. for (int i = mappings.size(); --i >= 0;)
  205. if (mappings.getUnchecked(i)->commandID == commandId)
  206. mappings.getUnchecked(i)->keypresses.removeAllInstancesOf (key);
  207. }
  208. }
  209. }
  210. return true;
  211. }
  212. return false;
  213. }
  214. XmlElement* KeyPressMappingSet::createXml (const bool saveDifferencesFromDefaultSet) const
  215. {
  216. ScopedPointer <KeyPressMappingSet> defaultSet;
  217. if (saveDifferencesFromDefaultSet)
  218. {
  219. defaultSet = new KeyPressMappingSet (commandManager);
  220. defaultSet->resetToDefaultMappings();
  221. }
  222. XmlElement* const doc = new XmlElement ("KEYMAPPINGS");
  223. doc->setAttribute ("basedOnDefaults", saveDifferencesFromDefaultSet);
  224. for (int i = 0; i < mappings.size(); ++i)
  225. {
  226. const CommandMapping& cm = *mappings.getUnchecked(i);
  227. for (int j = 0; j < cm.keypresses.size(); ++j)
  228. {
  229. if (defaultSet == nullptr
  230. || ! defaultSet->containsMapping (cm.commandID, cm.keypresses.getReference (j)))
  231. {
  232. XmlElement* const map = doc->createNewChildElement ("MAPPING");
  233. map->setAttribute ("commandId", String::toHexString ((int) cm.commandID));
  234. map->setAttribute ("description", commandManager.getDescriptionOfCommand (cm.commandID));
  235. map->setAttribute ("key", cm.keypresses.getReference (j).getTextDescription());
  236. }
  237. }
  238. }
  239. if (defaultSet != nullptr)
  240. {
  241. for (int i = 0; i < defaultSet->mappings.size(); ++i)
  242. {
  243. const CommandMapping& cm = *defaultSet->mappings.getUnchecked(i);
  244. for (int j = 0; j < cm.keypresses.size(); ++j)
  245. {
  246. if (! containsMapping (cm.commandID, cm.keypresses.getReference (j)))
  247. {
  248. XmlElement* const map = doc->createNewChildElement ("UNMAPPING");
  249. map->setAttribute ("commandId", String::toHexString ((int) cm.commandID));
  250. map->setAttribute ("description", commandManager.getDescriptionOfCommand (cm.commandID));
  251. map->setAttribute ("key", cm.keypresses.getReference (j).getTextDescription());
  252. }
  253. }
  254. }
  255. }
  256. return doc;
  257. }
  258. //==============================================================================
  259. bool KeyPressMappingSet::keyPressed (const KeyPress& key, Component* const originatingComponent)
  260. {
  261. bool commandWasDisabled = false;
  262. for (int i = 0; i < mappings.size(); ++i)
  263. {
  264. CommandMapping& cm = *mappings.getUnchecked(i);
  265. if (cm.keypresses.contains (key))
  266. {
  267. if (const ApplicationCommandInfo* const ci = commandManager.getCommandForID (cm.commandID))
  268. {
  269. if ((ci->flags & ApplicationCommandInfo::wantsKeyUpDownCallbacks) == 0)
  270. {
  271. ApplicationCommandInfo info (0);
  272. if (commandManager.getTargetForCommand (cm.commandID, info) != nullptr)
  273. {
  274. if ((info.flags & ApplicationCommandInfo::isDisabled) == 0)
  275. {
  276. invokeCommand (cm.commandID, key, true, 0, originatingComponent);
  277. return true;
  278. }
  279. else
  280. {
  281. commandWasDisabled = true;
  282. }
  283. }
  284. }
  285. }
  286. }
  287. }
  288. if (originatingComponent != nullptr && commandWasDisabled)
  289. originatingComponent->getLookAndFeel().playAlertSound();
  290. return false;
  291. }
  292. bool KeyPressMappingSet::keyStateChanged (const bool /*isKeyDown*/, Component* originatingComponent)
  293. {
  294. bool used = false;
  295. const uint32 now = Time::getMillisecondCounter();
  296. for (int i = mappings.size(); --i >= 0;)
  297. {
  298. CommandMapping& cm = *mappings.getUnchecked(i);
  299. if (cm.wantsKeyUpDownCallbacks)
  300. {
  301. for (int j = cm.keypresses.size(); --j >= 0;)
  302. {
  303. const KeyPress key (cm.keypresses.getReference (j));
  304. const bool isDown = key.isCurrentlyDown();
  305. int keyPressEntryIndex = 0;
  306. bool wasDown = false;
  307. for (int k = keysDown.size(); --k >= 0;)
  308. {
  309. if (key == keysDown.getUnchecked(k)->key)
  310. {
  311. keyPressEntryIndex = k;
  312. wasDown = true;
  313. used = true;
  314. break;
  315. }
  316. }
  317. if (isDown != wasDown)
  318. {
  319. int millisecs = 0;
  320. if (isDown)
  321. {
  322. KeyPressTime* const k = new KeyPressTime();
  323. k->key = key;
  324. k->timeWhenPressed = now;
  325. keysDown.add (k);
  326. }
  327. else
  328. {
  329. const uint32 pressTime = keysDown.getUnchecked (keyPressEntryIndex)->timeWhenPressed;
  330. if (now > pressTime)
  331. millisecs = (int) (now - pressTime);
  332. keysDown.remove (keyPressEntryIndex);
  333. }
  334. invokeCommand (cm.commandID, key, isDown, millisecs, originatingComponent);
  335. used = true;
  336. }
  337. }
  338. }
  339. }
  340. return used;
  341. }
  342. void KeyPressMappingSet::globalFocusChanged (Component* focusedComponent)
  343. {
  344. if (focusedComponent != nullptr)
  345. focusedComponent->keyStateChanged (false);
  346. }