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.

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