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.

105 lines
5.1KB

  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. #ifndef __JUCE_TEXTEDITORKEYMAPPER_JUCEHEADER__
  19. #define __JUCE_TEXTEDITORKEYMAPPER_JUCEHEADER__
  20. #include "juce_KeyPress.h"
  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. */
  26. template <class CallbackClass>
  27. struct TextEditorKeyMapper
  28. {
  29. /** Checks the keypress and invokes one of a range of navigation functions that
  30. the target class must implement, based on the key event.
  31. */
  32. static bool invokeKeyFunction (CallbackClass& target, const KeyPress& key)
  33. {
  34. const bool isShiftDown = key.getModifiers().isShiftDown();
  35. const bool ctrlOrAltDown = key.getModifiers().isCtrlDown() || key.getModifiers().isAltDown();
  36. if (key == KeyPress (KeyPress::downKey, ModifierKeys::ctrlModifier, 0) && target.scrollUp()) return true;
  37. if (key == KeyPress (KeyPress::upKey, ModifierKeys::ctrlModifier, 0) && target.scrollDown()) return true;
  38. #if JUCE_MAC
  39. if (key.getModifiers().isCommandDown())
  40. {
  41. if (key.isKeyCode (KeyPress::upKey)) return target.moveCaretToTop (isShiftDown);
  42. if (key.isKeyCode (KeyPress::downKey)) return target.moveCaretToEnd (isShiftDown);
  43. if (key.isKeyCode (KeyPress::leftKey)) return target.moveCaretToStartOfLine (isShiftDown);
  44. if (key.isKeyCode (KeyPress::rightKey)) return target.moveCaretToEndOfLine (isShiftDown);
  45. }
  46. #endif
  47. if (key.isKeyCode (KeyPress::upKey)) return target.moveCaretUp (isShiftDown);
  48. if (key.isKeyCode (KeyPress::downKey)) return target.moveCaretDown (isShiftDown);
  49. if (key.isKeyCode (KeyPress::leftKey)) return target.moveCaretLeft (ctrlOrAltDown, isShiftDown);
  50. if (key.isKeyCode (KeyPress::rightKey)) return target.moveCaretRight (ctrlOrAltDown, isShiftDown);
  51. if (key.isKeyCode (KeyPress::pageUpKey)) return target.pageUp (isShiftDown);
  52. if (key.isKeyCode (KeyPress::pageDownKey)) return target.pageDown (isShiftDown);
  53. if (key.isKeyCode (KeyPress::homeKey)) return ctrlOrAltDown ? target.moveCaretToTop (isShiftDown)
  54. : target.moveCaretToStartOfLine (isShiftDown);
  55. if (key.isKeyCode (KeyPress::endKey)) return ctrlOrAltDown ? target.moveCaretToEnd (isShiftDown)
  56. : target.moveCaretToEndOfLine (isShiftDown);
  57. if (key == KeyPress ('c', ModifierKeys::commandModifier, 0)
  58. || key == KeyPress (KeyPress::insertKey, ModifierKeys::ctrlModifier, 0))
  59. return target.copyToClipboard();
  60. if (key == KeyPress ('x', ModifierKeys::commandModifier, 0)
  61. || key == KeyPress (KeyPress::deleteKey, ModifierKeys::shiftModifier, 0))
  62. return target.cutToClipboard();
  63. if (key == KeyPress ('v', ModifierKeys::commandModifier, 0)
  64. || key == KeyPress (KeyPress::insertKey, ModifierKeys::shiftModifier, 0))
  65. return target.pasteFromClipboard();
  66. if (key.isKeyCode (KeyPress::backspaceKey)) return target.deleteBackwards (ctrlOrAltDown);
  67. if (key.isKeyCode (KeyPress::deleteKey)) return target.deleteForwards (ctrlOrAltDown);
  68. if (key == KeyPress ('a', ModifierKeys::commandModifier, 0))
  69. return target.selectAll();
  70. if (key == KeyPress ('z', ModifierKeys::commandModifier, 0))
  71. return target.undo();
  72. if (key == KeyPress ('y', ModifierKeys::commandModifier, 0)
  73. || key == KeyPress ('z', ModifierKeys::commandModifier | ModifierKeys::shiftModifier, 0))
  74. return target.redo();
  75. return false;
  76. }
  77. };
  78. #endif // __JUCE_TEXTEDITORKEYMAPPER_JUCEHEADER__