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.

125 lines
5.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. //==============================================================================
  22. /** This class is used to invoke a range of text-editor navigation methods on
  23. an object, based upon a keypress event.
  24. It's currently used internally by the TextEditor and CodeEditorComponent.
  25. @tags{GUI}
  26. */
  27. template <class CallbackClass>
  28. struct TextEditorKeyMapper
  29. {
  30. /** Checks the keypress and invokes one of a range of navigation functions that
  31. the target class must implement, based on the key event.
  32. */
  33. static bool invokeKeyFunction (CallbackClass& target, const KeyPress& key)
  34. {
  35. auto mods = key.getModifiers();
  36. const bool isShiftDown = mods.isShiftDown();
  37. const bool ctrlOrAltDown = mods.isCtrlDown() || mods.isAltDown();
  38. int numCtrlAltCommandKeys = 0;
  39. if (mods.isCtrlDown()) ++numCtrlAltCommandKeys;
  40. if (mods.isAltDown()) ++numCtrlAltCommandKeys;
  41. if (key == KeyPress (KeyPress::downKey, ModifierKeys::ctrlModifier, 0) && target.scrollUp()) return true;
  42. if (key == KeyPress (KeyPress::upKey, ModifierKeys::ctrlModifier, 0) && target.scrollDown()) return true;
  43. #if JUCE_MAC
  44. if (mods.isCommandDown() && ! ctrlOrAltDown)
  45. {
  46. if (key.isKeyCode (KeyPress::upKey)) return target.moveCaretToTop (isShiftDown);
  47. if (key.isKeyCode (KeyPress::downKey)) return target.moveCaretToEnd (isShiftDown);
  48. if (key.isKeyCode (KeyPress::leftKey)) return target.moveCaretToStartOfLine (isShiftDown);
  49. if (key.isKeyCode (KeyPress::rightKey)) return target.moveCaretToEndOfLine (isShiftDown);
  50. }
  51. if (mods.isCommandDown())
  52. ++numCtrlAltCommandKeys;
  53. #endif
  54. if (numCtrlAltCommandKeys < 2)
  55. {
  56. if (key.isKeyCode (KeyPress::leftKey)) return target.moveCaretLeft (ctrlOrAltDown, isShiftDown);
  57. if (key.isKeyCode (KeyPress::rightKey)) return target.moveCaretRight (ctrlOrAltDown, isShiftDown);
  58. if (key.isKeyCode (KeyPress::homeKey)) return ctrlOrAltDown ? target.moveCaretToTop (isShiftDown)
  59. : target.moveCaretToStartOfLine (isShiftDown);
  60. if (key.isKeyCode (KeyPress::endKey)) return ctrlOrAltDown ? target.moveCaretToEnd (isShiftDown)
  61. : target.moveCaretToEndOfLine (isShiftDown);
  62. }
  63. if (numCtrlAltCommandKeys == 0)
  64. {
  65. if (key.isKeyCode (KeyPress::upKey)) return target.moveCaretUp (isShiftDown);
  66. if (key.isKeyCode (KeyPress::downKey)) return target.moveCaretDown (isShiftDown);
  67. if (key.isKeyCode (KeyPress::pageUpKey)) return target.pageUp (isShiftDown);
  68. if (key.isKeyCode (KeyPress::pageDownKey)) return target.pageDown (isShiftDown);
  69. }
  70. if (key == KeyPress ('c', ModifierKeys::commandModifier, 0)
  71. || key == KeyPress (KeyPress::insertKey, ModifierKeys::ctrlModifier, 0))
  72. return target.copyToClipboard();
  73. if (key == KeyPress ('x', ModifierKeys::commandModifier, 0)
  74. || key == KeyPress (KeyPress::deleteKey, ModifierKeys::shiftModifier, 0))
  75. return target.cutToClipboard();
  76. if (key == KeyPress ('v', ModifierKeys::commandModifier, 0)
  77. || key == KeyPress (KeyPress::insertKey, ModifierKeys::shiftModifier, 0))
  78. return target.pasteFromClipboard();
  79. // NB: checking for delete must happen after the earlier check for shift + delete
  80. if (numCtrlAltCommandKeys < 2)
  81. {
  82. if (key.isKeyCode (KeyPress::backspaceKey)) return target.deleteBackwards (ctrlOrAltDown);
  83. if (key.isKeyCode (KeyPress::deleteKey)) return target.deleteForwards (ctrlOrAltDown);
  84. }
  85. if (key == KeyPress ('a', ModifierKeys::commandModifier, 0))
  86. return target.selectAll();
  87. if (key == KeyPress ('z', ModifierKeys::commandModifier, 0))
  88. return target.undo();
  89. if (key == KeyPress ('y', ModifierKeys::commandModifier, 0)
  90. || key == KeyPress ('z', ModifierKeys::commandModifier | ModifierKeys::shiftModifier, 0))
  91. return target.redo();
  92. return false;
  93. }
  94. };
  95. } // namespace juce