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.

418 lines
14KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. KeyPressMappingSet::KeyPressMappingSet (ApplicationCommandManager& cm)
  19. : commandManager (cm)
  20. {
  21. Desktop::getInstance().addFocusChangeListener (this);
  22. }
  23. KeyPressMappingSet::KeyPressMappingSet (const KeyPressMappingSet& other)
  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. if (containsMapping (commandId, key))
  205. removeKeyPress (key);
  206. }
  207. }
  208. }
  209. return true;
  210. }
  211. return false;
  212. }
  213. XmlElement* KeyPressMappingSet::createXml (const bool saveDifferencesFromDefaultSet) const
  214. {
  215. ScopedPointer <KeyPressMappingSet> defaultSet;
  216. if (saveDifferencesFromDefaultSet)
  217. {
  218. defaultSet = new KeyPressMappingSet (commandManager);
  219. defaultSet->resetToDefaultMappings();
  220. }
  221. XmlElement* const doc = new XmlElement ("KEYMAPPINGS");
  222. doc->setAttribute ("basedOnDefaults", saveDifferencesFromDefaultSet);
  223. for (int i = 0; i < mappings.size(); ++i)
  224. {
  225. const CommandMapping& cm = *mappings.getUnchecked(i);
  226. for (int j = 0; j < cm.keypresses.size(); ++j)
  227. {
  228. if (defaultSet == nullptr
  229. || ! defaultSet->containsMapping (cm.commandID, cm.keypresses.getReference (j)))
  230. {
  231. XmlElement* const map = doc->createNewChildElement ("MAPPING");
  232. map->setAttribute ("commandId", String::toHexString ((int) cm.commandID));
  233. map->setAttribute ("description", commandManager.getDescriptionOfCommand (cm.commandID));
  234. map->setAttribute ("key", cm.keypresses.getReference (j).getTextDescription());
  235. }
  236. }
  237. }
  238. if (defaultSet != nullptr)
  239. {
  240. for (int i = 0; i < defaultSet->mappings.size(); ++i)
  241. {
  242. const CommandMapping& cm = *defaultSet->mappings.getUnchecked(i);
  243. for (int j = 0; j < cm.keypresses.size(); ++j)
  244. {
  245. if (! containsMapping (cm.commandID, cm.keypresses.getReference (j)))
  246. {
  247. XmlElement* const map = doc->createNewChildElement ("UNMAPPING");
  248. map->setAttribute ("commandId", String::toHexString ((int) cm.commandID));
  249. map->setAttribute ("description", commandManager.getDescriptionOfCommand (cm.commandID));
  250. map->setAttribute ("key", cm.keypresses.getReference (j).getTextDescription());
  251. }
  252. }
  253. }
  254. }
  255. return doc;
  256. }
  257. //==============================================================================
  258. bool KeyPressMappingSet::keyPressed (const KeyPress& key, Component* const originatingComponent)
  259. {
  260. bool commandWasDisabled = false;
  261. for (int i = 0; i < mappings.size(); ++i)
  262. {
  263. CommandMapping& cm = *mappings.getUnchecked(i);
  264. if (cm.keypresses.contains (key))
  265. {
  266. if (const ApplicationCommandInfo* const ci = commandManager.getCommandForID (cm.commandID))
  267. {
  268. if ((ci->flags & ApplicationCommandInfo::wantsKeyUpDownCallbacks) == 0)
  269. {
  270. ApplicationCommandInfo info (0);
  271. if (commandManager.getTargetForCommand (cm.commandID, info) != nullptr)
  272. {
  273. if ((info.flags & ApplicationCommandInfo::isDisabled) == 0)
  274. {
  275. invokeCommand (cm.commandID, key, true, 0, originatingComponent);
  276. return true;
  277. }
  278. else
  279. {
  280. commandWasDisabled = true;
  281. }
  282. }
  283. }
  284. }
  285. }
  286. }
  287. if (originatingComponent != nullptr && commandWasDisabled)
  288. originatingComponent->getLookAndFeel().playAlertSound();
  289. return false;
  290. }
  291. bool KeyPressMappingSet::keyStateChanged (const bool /*isKeyDown*/, Component* originatingComponent)
  292. {
  293. bool used = false;
  294. const uint32 now = Time::getMillisecondCounter();
  295. for (int i = mappings.size(); --i >= 0;)
  296. {
  297. CommandMapping& cm = *mappings.getUnchecked(i);
  298. if (cm.wantsKeyUpDownCallbacks)
  299. {
  300. for (int j = cm.keypresses.size(); --j >= 0;)
  301. {
  302. const KeyPress key (cm.keypresses.getReference (j));
  303. const bool isDown = key.isCurrentlyDown();
  304. int keyPressEntryIndex = 0;
  305. bool wasDown = false;
  306. for (int k = keysDown.size(); --k >= 0;)
  307. {
  308. if (key == keysDown.getUnchecked(k)->key)
  309. {
  310. keyPressEntryIndex = k;
  311. wasDown = true;
  312. used = true;
  313. break;
  314. }
  315. }
  316. if (isDown != wasDown)
  317. {
  318. int millisecs = 0;
  319. if (isDown)
  320. {
  321. KeyPressTime* const k = new KeyPressTime();
  322. k->key = key;
  323. k->timeWhenPressed = now;
  324. keysDown.add (k);
  325. }
  326. else
  327. {
  328. const uint32 pressTime = keysDown.getUnchecked (keyPressEntryIndex)->timeWhenPressed;
  329. if (now > pressTime)
  330. millisecs = (int) (now - pressTime);
  331. keysDown.remove (keyPressEntryIndex);
  332. }
  333. invokeCommand (cm.commandID, key, isDown, millisecs, originatingComponent);
  334. used = true;
  335. }
  336. }
  337. }
  338. }
  339. return used;
  340. }
  341. void KeyPressMappingSet::globalFocusChanged (Component* focusedComponent)
  342. {
  343. if (focusedComponent != nullptr)
  344. focusedComponent->keyStateChanged (false);
  345. }