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.

119 lines
5.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI 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. #pragma once
  18. //==============================================================================
  19. /** This class is used to invoke a range of text-editor navigation methods on
  20. an object, based upon a keypress event.
  21. It's currently used internally by the TextEditor and CodeEditorComponent.
  22. */
  23. template <class CallbackClass>
  24. struct TextEditorKeyMapper
  25. {
  26. /** Checks the keypress and invokes one of a range of navigation functions that
  27. the target class must implement, based on the key event.
  28. */
  29. static bool invokeKeyFunction (CallbackClass& target, const KeyPress& key)
  30. {
  31. const ModifierKeys& mods = key.getModifiers();
  32. const bool isShiftDown = mods.isShiftDown();
  33. const bool ctrlOrAltDown = mods.isCtrlDown() || mods.isAltDown();
  34. int numCtrlAltCommandKeys = 0;
  35. if (mods.isCtrlDown()) ++numCtrlAltCommandKeys;
  36. if (mods.isAltDown()) ++numCtrlAltCommandKeys;
  37. if (key == KeyPress (KeyPress::downKey, ModifierKeys::ctrlModifier, 0) && target.scrollUp()) return true;
  38. if (key == KeyPress (KeyPress::upKey, ModifierKeys::ctrlModifier, 0) && target.scrollDown()) return true;
  39. #if JUCE_MAC
  40. if (mods.isCommandDown() && ! ctrlOrAltDown)
  41. {
  42. if (key.isKeyCode (KeyPress::upKey)) return target.moveCaretToTop (isShiftDown);
  43. if (key.isKeyCode (KeyPress::downKey)) return target.moveCaretToEnd (isShiftDown);
  44. if (key.isKeyCode (KeyPress::leftKey)) return target.moveCaretToStartOfLine (isShiftDown);
  45. if (key.isKeyCode (KeyPress::rightKey)) return target.moveCaretToEndOfLine (isShiftDown);
  46. }
  47. if (mods.isCommandDown())
  48. ++numCtrlAltCommandKeys;
  49. #endif
  50. if (numCtrlAltCommandKeys < 2)
  51. {
  52. if (key.isKeyCode (KeyPress::leftKey)) return target.moveCaretLeft (ctrlOrAltDown, isShiftDown);
  53. if (key.isKeyCode (KeyPress::rightKey)) return target.moveCaretRight (ctrlOrAltDown, isShiftDown);
  54. if (key.isKeyCode (KeyPress::homeKey)) return ctrlOrAltDown ? target.moveCaretToTop (isShiftDown)
  55. : target.moveCaretToStartOfLine (isShiftDown);
  56. if (key.isKeyCode (KeyPress::endKey)) return ctrlOrAltDown ? target.moveCaretToEnd (isShiftDown)
  57. : target.moveCaretToEndOfLine (isShiftDown);
  58. }
  59. if (numCtrlAltCommandKeys == 0)
  60. {
  61. if (key.isKeyCode (KeyPress::upKey)) return target.moveCaretUp (isShiftDown);
  62. if (key.isKeyCode (KeyPress::downKey)) return target.moveCaretDown (isShiftDown);
  63. if (key.isKeyCode (KeyPress::pageUpKey)) return target.pageUp (isShiftDown);
  64. if (key.isKeyCode (KeyPress::pageDownKey)) return target.pageDown (isShiftDown);
  65. }
  66. if (key == KeyPress ('c', ModifierKeys::commandModifier, 0)
  67. || key == KeyPress (KeyPress::insertKey, ModifierKeys::ctrlModifier, 0))
  68. return target.copyToClipboard();
  69. if (key == KeyPress ('x', ModifierKeys::commandModifier, 0)
  70. || key == KeyPress (KeyPress::deleteKey, ModifierKeys::shiftModifier, 0))
  71. return target.cutToClipboard();
  72. if (key == KeyPress ('v', ModifierKeys::commandModifier, 0)
  73. || key == KeyPress (KeyPress::insertKey, ModifierKeys::shiftModifier, 0))
  74. return target.pasteFromClipboard();
  75. // NB: checking for delete must happen after the earlier check for shift + delete
  76. if (numCtrlAltCommandKeys < 2)
  77. {
  78. if (key.isKeyCode (KeyPress::backspaceKey)) return target.deleteBackwards (ctrlOrAltDown);
  79. if (key.isKeyCode (KeyPress::deleteKey)) return target.deleteForwards (ctrlOrAltDown);
  80. }
  81. if (key == KeyPress ('a', ModifierKeys::commandModifier, 0))
  82. return target.selectAll();
  83. if (key == KeyPress ('z', ModifierKeys::commandModifier, 0))
  84. return target.undo();
  85. if (key == KeyPress ('y', ModifierKeys::commandModifier, 0)
  86. || key == KeyPress ('z', ModifierKeys::commandModifier | ModifierKeys::shiftModifier, 0))
  87. return target.redo();
  88. return false;
  89. }
  90. };